Swift 2, convert a String into an Int - swift2

I'm using Xcode 7 Beta 3 and reading through Swift 2.2 document. I'm trying to compile this example found in the Basics section of the document:
let possibleNumber = "123"
let convertedNumber = Int(possibleNumber)
It is supposed to convert a string into an optional int. However Xcode gives the error:
Cannot call value of non-function type 'int'
I was working on Xcode 7.2.1, then I knew that Swift 2.2 is packaged with Xcode 7.3 Beta 3, so I downloaded that to try, but the same error happens.
So, is the document wrong? and how to achieve the string into int conversion?

There is nothing wrong with your code, I tested and ran your exact code in Xcode 7.2:
let possibleNumber = "123"
let convertedNumber = Int(possibleNumber)
print("\(convertedNumber)")
It complied, ran within an app of mine, and printed the Int value 123.
Perhaps the error is being thrown from another area of code in your Xcode app, other then the code you think is throwing the error....
Perhaps you are not referencing the version of Swift you think you are...
Note, you should use if let with any conversion attempt:
if let convertedNumber = Int(possibleNumber) {
}
The if let should be used for a conversion no matter how remote the possibility of failure.

Related

Array and Dictionary type declarations in Swift

According to my understanding of the documentation, this should be correct:
var cookies: [NSHTTPCookie] = NSHTTPCookieStorage.sharedHTTPCookieStorage().cookies as [NSHTTPCookie]
where I'm creating an array of NSHTTPCookie objects. The interpreter does not like this syntax, however, giving me "Expected type after 'as'" and putting a little pointer at the opening bracket of the [NSHTTPCookie] at the end.
However, this works:
var cookies:NSHTTPCookie[] = NSHTTPCookieStorage.sharedHTTPCookieStorage().cookies as NSHTTPCookie[]
From the documentation, it seems like the first version is more correct, however.
Here's another example, this time with someone else's code. No one else using this code has reported the same behavior I get. (This is just a snippet; if the context is relevant let me know and I'll post more)
func asDict(x: AnyObject) -> [String:AnyObject]? {
return x as? [String:AnyObject]
}
In this case the playground interpreter objects in both places [String:AnyObject] is used. It just doesn't seem to be recognizing it as a type.
I double-checked to make sure I have the most recent beta of Xcode 6, but it seems much more likely to me that the problem is in my understanding rather than in the tool, since this would be a mighty big bug for only me to experience.
You must be using an old beta, this works in Beta 5 playground:
import Foundation
println("hello")
var cookies:[NSHTTPCookie] = NSHTTPCookieStorage.sharedHTTPCookieStorage().cookies as [NSHTTPCookie]
println("goodbye")

How to force a raw value of 7 into a UIViewAnimationCurve enum?

The key UIKeyboardAnimationCurveUserInfoKey in the userInfo dictionary of a UIKeyboardWillShowNotification contains an Int with the value 7.
Now I need to pass this Int into UIView.setAnimationCurve(<here>). I tried to create the required UIViewAnimationCurve enum like this UIViewAnimationCurve(rawValue: 7). Because the raw value 7 is undocumented, the result is always nil.
It works fine this way in Objective-C. Any idea how to get this animation curve from the notification into a UIView animation using Swift?
Update:
As pointed out by Martin, this is no longer a problem since Xcode 6.3.
From the Xcode 6.3 Release Notes:
Imported NS_ENUM types with undocumented values, such as UIViewAnimationCurve, can now be converted from their raw integer values using the init(rawValue:) initializer without being reset to nil. Code that used unsafeBitCast as a workaround for this issue can be written to use the raw value initializer.
I think I figured it out, but I'm not sure if this is the way it's supposed to be done.
let animationCurve = unsafeBitCast(7, UIViewAnimationCurve.self)
UIView.setAnimationCurve(animationCurve)
Update: The solution contained in this question works as well.
var animationCurve = UIViewAnimationCurve.EaseInOut
NSNumber(integer: 7).getValue(&animationCurve)

Swift NSDate Xcode error - SourceKitService terminated

import Foundation
var currentTime = NSDate()
println("It is currently", currentTime)
This Swift code is very simple and should work, correct? Why am i receiving an error that says "SourceKitService terminated - editor functionality currently limited"
Am I doing something wrong or is it the beta's fault?
You would use string interpolation as Jack Wu suggested in the first comment:
println("It is currently \(currentTime)")
The println primary function does not take multiple arguments. You could also use
println(currentTime)
However, the fact that your first (syntax error) attempt causes Xcode 6 to crash (at least it does for me) is certainly a bug. You should just get an issue reported.

xcode 5.1 error implicit conversion loses precision

I updated to Xcode 5.1 and can no longer build several of my projects which use Core Plot 1.4, complaining generally about garbage collection, and proposing that I convert to ARC. I complied, but there were several statements that could not be converted. I quickly came to SO to find a solution, and I found a promising one here:
Core Plot and Xcode 5.1 - How to convert Core Plot to ARC?
I followed this suggestion, and it worked for the conversion to ARC. However, I was now left with 2 errors (not warnings) in CPTTextStylePlatformSpecific.m, which complained: “Implicit conversion loses integer precision: 'NSTextAlignment' (aka 'unsigned long') to 'CPTTextAlignment' (aka 'enum _CPTTextAlignment’)”. This issue had not appeared when building the project before the Xcode update.
The offending code:
// Text alignment and line break mode
NSParagraphStyle *paragraphStyle = [attributes valueForKey:NSParagraphStyleAttributeName];
if ( paragraphStyle ) {
newStyle.textAlignment = paragraphStyle.alignment;
newStyle.lineBreakMode = paragraphStyle.lineBreakMode;
}
return [[newStyle copy] autorelease];
And here:
// Text alignment and line break mode
NSParagraphStyle *paragraphStyle = [attributes valueForKey:NSParagraphStyleAttributeName];
if ( paragraphStyle ) {
newStyle.textAlignment = paragraphStyle.alignment;
newStyle.lineBreakMode = paragraphStyle.lineBreakMode;
}
return newStyle;
In both cases, the error was on the line
newStyle.textAlignment = paragraphStyle.alignment;
I am guessing the enum is an integer, and the integer to long assignment is the issue. Seems like it merits a warning, not an error. Is there a compiler flag I can set to achieve this? Or is there a bigger issue I am missing?
I had exactly this issue and found that in the CorePlot project, that I had imported into my project, I had the "Apple LLVM 5.1 - Warning Policies", "Treat Warnings as Errors" set you "Yes". I still get the warning but at least I can build and submit my project.
This is still not ideal and I would really like a proper solution - I suppose I'll just have to keep checking the CorePlot repository for updates.

How to make GDATAXML compatible with ARC in XCODE 4.2?

I tried to convert GDATAXML Lib to ARC automatically with the refractor -> Convert to ARC Objective-C in XCode 4.2.
The ARC converter gives the following error:
result = [NSString stringWithUTF8String:(const char *) chars];
if (cacheDict) {
// save the string in the document's string cache
CFDictionarySetValue(cacheDict, chars, result);
}
error: Impicit conversion of Ojective-C pointer to void.
Has anyone succeeded in converting the GDATAXML Libs to ARC Objective-C?
please follow the instructions on how to make GDataXML work on your code: http://ibombsite.blogspot.com/2012/02/how-to-make-gdataxmlnode-work.html
I found someone who has (apparently successfully) done the refactor for ARC.
see: http://www.michaelbabiy.com/arc-compliant-gdataxml-library/
You need to use bridged cast:CFDictionarySetValue(cacheDict, chars, (__bridge_retained_void*) result);Check out Apple's article "Transitioning to ARC", especially the part about briding.

Resources