Compiler Error expression was too complex to be resolved - ios

I am getting this error:
Expression was too complex to be resolved in reasonable time
Please help me out. What should I do? I am using the same line in the previous view controller and it's working perfectly.
let url = URL(string: self.con+"loc?email="+email+"&lat="+lati+"&log="+logi!)

The reason behind this error is that Xcode gets confused when you use too many + signs. Always try to use the String Interpolation:
let paramsStr = "loc?email=\(email)&lat=\(lati)&log=\(logi)"
Also a good read about this topic:
https://stackoverflow.com/a/29931329/3403364

Break it down to smaller expressions. Swift compiler is too dumb to understand your expression ))
Like this:
let paramsStr = "loc?email=" + email + "&lat=" + lati + "&log=" + logi
let url = URL(string: self.con + paramsStr)

Related

Creating CGPDFDocument from a URL yields errors in Swift 3?

The below block of code yields these two errors when I try to build. Can anybody help me out? Xcode 8 had its way with my project using its migrator and I haven't seen this error before.
let url = URL(string: "http://www.google.com")!;
var pdf:CGPDFDocument = CGPDFDocument(url);
error: cannot invoke initializer for type 'CGPDFDocument' with an argument list of type '(URL)'
note: overloads for 'CGPDFDocument' exist with these partially matching parameter lists: (CGDataProvider), (CFURL)
Hello it should like this below.
let url = URL(string: "http://www.google.com")!
fileprivate var pdfDoc: CGPDFDocument
pdfDoc = CGPDFDocument(url as CFURL)!

Swift 3: Joining multiple Strings with "+" operator no longer possible

In swift 2.3 I had this working simple piece of code:
let joinedString = partOne! + PartTwo! + PartThree! + PartFour!
Now with the conversion to swift 3 I've been bashing my head in over about 24 errors out of the blue with the most vague explanations.. This is one of them:
The same line of code gives error:
Ambiguous reference to member '+'
However if I split them up like so:
let OneAndTwo = partOne! + partTwo!
let ThreeAndFour = partThree! + PartFour!
let joinedString = OneAndTwo + ThreeAndFour
This works... Did they remove linking multiple strings like this or is it buggy? Seems like the compiler thinks the '+' is a variable or something else named the same?
EDIT:
Even though it's another error this seems to be related to: This Question
Also crashes once you go upwards of 2 optional strings. I guess optional binding is the way to go then. Seems like this bug has been there for quite some time.
This seems like a bug and I'll investigate further. If we simulate the behaviour of ! with another operator it works just fine:
postfix operator |! {}
postfix func |! <T>(rhs: T?) -> T {
return rhs!
}
let s1: String? = "Hello"
let s2: String? = " "
let s3: String? = "World"
let joined = s1|! + s2|! + s3|! // "Hello World"

String encoding - Swift

I am using the following method to encode a string in objective - C:
+(NSString*)urlEncoded:(NSString*)string
{ return ((NSString *)CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes (NULL,(CFStringRef)string, NULL,(CFStringRef)#"! '=();#&+$%#",kCFStringEncodingUTF8 )));}
Is there any counterpart for this method in Swift 2.0 ? I have tried using many solutions present on stack, but none of them could solve my problem.
You probably want to use stringByAddingPercentEncodingWithAllowedCharacters with NSCharacterSet's URL-component specific character sets:
let title = "NSURL / NSURLComponents"
let escapedTitle = title.stringByAddingPercentEncodingWithAllowedCharacters(NSCharacterSet.URLQueryAllowedCharacterSet())
The idea behind those character sets is that they're probably more correct for the uses they describe than any finite set you have. That said, you could use the same method with a set you generate yourself:
let escapeSet = NSCharacterSet(charactersInString: "! '=();#&+$%#")
let string = "sdfT&*w5e#sto([+peW7)%y9pqf])"
let escapedString = string.stringByAddingPercentEncodingWithAllowedCharacters(escapeSet.invertedSet)
// "sdfT%26*w5e%23sto%28[%2BpeW7%29%25y9pqf]%29"

ios swift concatenating function results with another string

For multiline button text I believe you add "\n" to the string. However I'm having trouble concatenating my function results and the newlinetext
setTitle:
HomeVC.getFriendCount("2",id:"friendid") + "\n newlinetext"
I need help getting my function results concatenated with "\n newlinetext"
You didn't specify an error, so I'm not sure, but I'm betting getFriendCount returns a number.
Try this:
let count = HomeVC.getFriendCount("2",id:"friendid")
let title = "\(count)\n newlinetext"
I've already encountered this. There must be some problem with (string + string) because it just ignores \n, though I never understood why this is. You can fix it by using join function:
let stringsToJoin = [getFriendCount("2",id:"friendid"), "newlinetext"]
let nString = join("\n", stringsToJoin)
Hope it helps!
You can use NSString's stringByAppendingString method
let aString = NSString(string: HomeVC.getFriendCount("2",id:"friendid"))
let concatenatedString = aString.stringByAppendingString("\n newlinetext")
If your method
HomeVC.getFriendCount("2",id:"friendid")
returns and optional string, then you need to unwrap it before concatenating.
Try
HomeVC.getFriendCount("2",id:"friendid")! + "\n newlinetext"

arithmetic within string literal in Swift

Playing with Swift, I found something awkward error.
let cost = 82.5
let tip = 18.0
let str = "Your total cost will be \(cost + tip)"
This works fine as I expect, but
let cost = 82.5
let tip:Float = 18
let str = "Your total cost will be \(cost + tip)"
would not work with error
could not find member 'convertFromStringInterpolationSegment'
let str = "Your total cost will be \(cost + tip)"
The difference between two example is declaring tip constant to explicitly float or not. Would this be reasonable result?
You still need to cast the numbers into the same type so they can be added together, e.g:
let cost = 82.5
let tip:Float = 18
let str = "Your total cost will be \(Float(cost) + tip)"
By default real number literals are inferred as Double, i.e:
let cost:Double = 82.5
So they need to be either explicitly cast to a Double or a Float to be added together.
Values are never implicitly converted to another type.
let cost = 82.5
let tip:Float = 18
let str = "Your total cost will be \(cost + tip)"
In the above example it is considering cost as double & you have defined tip as float, so it is giving error.
Rather specify the type of cost as float as shown below
let cost:Float = 82.5
Hope it will solve your problem.
In your code cost in inferred to be of type Double.
In your first (working) example tip is also inferred to be Double and the expressions cost + tip is an addition of two Double values, resulting in a Double value.
In your second (not working) example tip is declared to be Float therefore the expressions cost + tip is an error.
The error message is not very informative. But the problem is that you are adding a Double to a Float and in a strongly statically typed language you will not have automatic type conversions like you had in C or Objective C.
You have to do either Float(cost) + tip or cost + Double(tip)

Resources