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.
Related
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.
It appears that XCtest "self measureBlock" is limited to milliseconds and 10 runs of the test code. Are there any ways to modify the behavior of measureBlock for more runs and/or nano or microsecond accuracy?
TL;DR
Apple provides a way to modify the behavior of measureBlock: by providing extra string constants but they don't support any string constants other than the default.
Long explanation
measureBlock calls the following function
- (void)measureMetrics:(NSArray *)metrics automaticallyStartMeasuring:(BOOL)automaticallyStartMeasuring withBlock:(void (^)(void))block;
//The implementation looks something like this (I can't say 100% but i'm pretty sure):
- (void)measureBlock:(void (^)(void))block {
NSArray<NSString *> *metrics = [[self class] defaultPerformanceMetrics];
[self measureMetrics:metrics automaticallyStartMeasure:YES withBlock:block];
}
defaultPerformanceMetrics is a class function that returns an array of strings.
From the Xcode source
"Subclasses can override this to change the behavior of
-measureBlock:"
Lovely, that sounds promising; we have customization behavior right? Well, they don't give you any strings to return. The default is XCTPerformanceMetric_WallClockTime ("com.apple.XCTPerformanceMetric_WallClockTime")
It turns out there aren't any string constants to return besides that one.
See the slides for WWDC 2014 Session 414 Testing in Xcode 6 (link).
I quote from page 158:
Currently supports one metric: XCTPerformanceMetric_WallClockTime
Nothing else has been added in Xcode 7 so it seems you're out of luck trying to modify measureBlock, sorry.
I've never found measureBlock: very useful. Check out Tidbits.xcodeproj/TidbitsTestBase/TBTestHelpers/comparePerformance https://github.com/tipbit/tidbits if you'd like to look at an alternative.
I've been writing an app that involves using NSUserDefaults to store a few Int variables and it's been working fine. I thought I was finished and was doing some final testing and one of the first lines of code that I wrote, and that has been working consistently before, has failed me.
Apparently the green line error is supposed to occur if I try to unwrap an optional that has a value of nil, but this variable is still very much an optional
var savedTotalSeconds: Int? = userDefaults.objectForKey("totalSecondsKey") as Int?
Why would this possibly return an error? It was working fine before and I only changed things I thought were unrelated to it. In the app I have a button to remove this stored value via:
userDefaults.removeObjectForKey("totalSecondsKey")
What could possibly have gone wrong?
Try using 'as? Int' instead of 'as Int?'
The difference is that the first one tries, and might fail, at casting to Int. That failure will be captured in the optionality of the resulting variable.
The second one tries to coerce the object to 'Int?'.
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")
I've got the following problem:
I've written my first Swift App (for iOS7) and it worked fine.
After changing some minor detail (adding a string somewhere) it wouldn't compile anymore, even if I changed everything back how it was before.
There is no error message or anything like it, it says that it's building the project (Compiling Swift Source Files) but it's not progressing at all, even after hours of "building".
I've tried it with Xcode 6 b1 and b2 and with both it's the same: all the other projects are compiling without any problems, this one get's stuck.
Does anyone have a clue what might be the problem and how to solve it?
Debug the code manually works for me.
Finally I find the root cause of my problem is too many string concatenation in one line.
Bug code:
var string = string1 + string2 + string3 + string4 + string5 + string6 + string7 + string8
Fixed code:
var string = string1
string += string2
string += string3
string += string4
string += string5
string += string6
string += string7
string += string8
Xcode 6 Beta sometimes does not show any error but there will be some errors in your code. Because of that it does not compile.
Try to comment different parts of code and then try to compile. You have to find out the error manually.
I had this issue because I had some errors in my code but it was not showing.
Debug it manually. All the best.
Xcode 6 Beta 5 went into a tailspin for me immediately after I wrote out an expression to concatenate 3 strings and an NSDate object with the "+" operator.
Wouldn't compile and got stuck indexing.
Search your code for long string concats and remove for now. This is clearly a bug.
Several things you can try:
Clean the project: Product -> Clean
Go to Product try other options such as Analyze or Profile, see if it still stuck on build.
Restart xcode
Reboot System
Open system console and try to trace the problem.
Last but most importantly, really, because they are beta version, there will be some unexpected bugs. If it still cannot be solved, please report it to Apple and expect it to be fixed in beta 3.
Based on your comment, go to Terminal and type in: defaults write com.apple.dt.XCode IDEIndexDisable 1
This bug will relate to our project state and source code.
I rolled back some commits of my project, xcode succeeded indexing my project.
In my case, xcode failed to index, when my project has a declaration of large dictionary.
(I succeed indexing after removing it.)