Here is my code . I am not sure is it best practice or not?  Please help me to clean my code. I initial all value. Should O put all in optional ?. Is there any alternative option? I can do that for sweeter and clean code.  :class Shop  {    var shopId : NSNumber = 0    var imgUrl: String?    var rating : Float = 0.0    var deliveryRate : String =     var minOrder : Float?    init(shop : Dictionary<String,AnyObject>) {        if let shopId =  shop[Constants.shopResponseKeys.ShopId] as? NSNumber {            self.shopId = shopId        }        else {            PrintConsole.NotFoundKey(key: Constants.shopResponseKeys.ShopId)        }        if let imgUrl =  shop[Constants.shopResponseKeys.ImgUrl]  as? String {            self.imgUrl = imgUrl        }        else {            PrintConsole.NotFoundKey(key: Constants.shopResponseKeys.ImgUrl)        }        if let rating =  shop[Constants.shopResponseKeys.Rating] as? Float {            self.rating = rating        }else {            PrintConsole.NotFoundKey(key: Constants.shopResponseKeys.Rating)        }        if let deliveryRate =  shop[Constants.shopResponseKeys.DeliveryRate] as? String {            self.deliveryRate = deliveryRate        }        else if let deliveryRate =  shop[Constants.shopResponseKeys.DeliveryRate] as? Int {            self.deliveryRate = \(deliveryRate)        }else {            PrintConsole.NotFoundKey(key: Constants.shopResponseKeys.DeliveryRate)        }    }}Here is my API Success response code :class NetworkOperation{    func GetShops(city : String , town : String ) {        let _jsonFormatString = [ city : city , town : town ]        var  _shops = [Shop]()        RestApiService.fetchPostRequestApps(with: _jsonFormatString) { (json) in            guard let shops = json[Constants.shopResponseKeys.Shops] as? [[String : AnyObject]] else {                PrintConsole.NotFoundKey(key: Constants.shopResponseKeys.Shops)                return            }            for shop in shops {                let shop = Shop(shop: shop)                _shops.append(shop)            }            print(_shops)        }    }}
How can I clean my swift code
swift;swift3
Formatting The readability of your code will be greatly improved if you stick to common conventions for use of white space and labelling.The biggest difference from convention in your code is how you use colon ':' and comma ',' symbol.Typically people use it with no space on the left, and one space to the right. So instead of:var minOrder : Float?init(shop : Dictionary<String,AnyObject>) { ... }let _jsonFormatString = [ city : city , town : town ]It's preferred to do this: var minOrder: Float?init(shop: Dictionary<String, AnyObject>) { ... }let _jsonFormatString = [city: city, town: town]CommentsPlease get into the habit of writing brief comments explaining the intent of what your code is doing, and for example to group required and optional properties. It will help people in future code reviews give you the best help, but will also be useful for yourself when you come back to read code you've written in the past.Swift conventionsYou're creating Shop objects as a result from some remote API call.I'd suggest that your shop type should be based on a struct rather than a class where properties are assigned as let rather than var. All of that is indicating that your Shop type is just a snapshot representation of the actual shop information on your server somewhere. I also assume that your RestApiService type returns a dictionary with values that can be cast as Int and Double (rather than NSNumber and Float). Unless you specifically need to use NSNumber or Float it's probably better to stick with these types.Instead of providing default values, a better pattern would be to have shop be created by a fallible initializer, if you don't have one of the properties required by the shop the initializer returns nil to indicate that it couldn't be created.struct Shop {    // required properties    let shopId: Int    let rating: Double    let deliveryRate: String    // optional properties    let imgUrl: String?    init?(fromAPIResponse response: Dictionary<String, AnyObject>) {        // ensure that required properties exist in the API response        guard            let shopId = response[Constants.shopResponseKeys.ShopId] as? Int,            let rating = response[Constants.shopResponseKeys.Rating] as? Double,            let deliveryRate = response[Constants.shopResponseKeys.DeliveryRate] as? String        else { return nil }        self.shopId = shopId        self.rating = rating        self.deliveryRate = deliveryRate        // assign optional properties outside of the guard        self.imgUrl = response[Constants.shopResponseKeys.ImgUrl] as? String    }}Having a more concise structure makes it easier to spot errors and be clearer about your intent. For example in writing this I noticed that minOrder is never assigned, which I assume is not intended.Using the initializer that returns nil if the Shop couldn't be created allows you to clean up how you manage your API response a bit by using flatMap get return only Shop that evaluate as non-nil:var shops: [Shop]RestApiService.fetchPostRequestApps(with: _jsonFormatString) { (json) in    guard let shopResponses = json[Constants.shopResponseKeys.Shops] as? [[String: AnyObject]] else {        PrintConsole.NotFoundKey(key: Constants.shopResponseKeys.Shops)        return    }    shops = shopResponses.flatMap{( shopInfo in        return Shop(fromAPIResponse: shopInfo)    )}    print(shops)}There's a lot happening here, so hope this helps point you in the direction of writing cleaner and more readable code in Swift!Reading Take a look at https://github.com/raywenderlich/swift-style-guide for an example of some pretty common conventions (or just be more observant of how the Swift team writes code).And also Swift API design guidelines for advice from swift team of practices for writing swifthttps://swift.org/blog/swift-3-api-design/