Search code examples
swiftstringsingletonstatic-data

How to break up expression into distinct sub-expressions?


I got this error in Swift "The compiler is unable to type-check this expression in reasonable time; try breaking up the expression into distinct sub-expressions"

Here is the code that is throwing the error.

             let obj = self.AddressList[indexPath.row] as! Address
//            let a:String! = obj.street+" ,"+obj.city+" "+obj.country
//            let b:String! = obj.country
            StaticData.singleton.cardAddress = obj.street+" ,"+obj.city+" "+obj.country
            StaticData.singleton.AddressID = obj.id
            StaticData.singleton.cart_addresstotal = obj.total_amount
            StaticData.singleton.cart_addressFee = obj.delivery_fee
            DispatchQueue.main.async {
                self.dismiss(animated:true, completion:nil)
            }

in particular it is this portion of the code in which the error is pointing towards:

    StaticData.singleton.cardAddress = obj.street+" ,"+obj.city+" "+obj.country

Solution

  • Just replace the + operator with String concatenation. In general, using String concatenation is the preferred approach over using the + operator.

    StaticData.singleton.cardAddress = "\(obj.street) ,\(obj.city) \(obj.country)"