Issue with String initializer in Swift when using format parameter - ios

I'm getting an error for this line of Swift code in my XCode playground:
print(String(format: "%.2f", 3.345))
The error reads
No exact matches in call to initializer
I believe this means that I haven't used the right parameter names/order to call the initializer. However, when running this line of code while working on an iOS app or even in the online Swift playground http://online.swiftplayground.run/, the line runs without any issues.
When running it in the XCode playground or on my terminal through the Swift REPL however, it throws an error.
Why is this the case?

Foundation has to be imported before using String.
String is a Foundation type in swift.
I was corrected by Matt below in the comments.
String is built into swift and String(format: is in foundation.
I guess that's why the docs didn't show it as such.
Thank you for the correction.

Related

Strange errors after converting SceneKit sample code to Swift 3

I downloaded Apple's SceneKit sample code (fox.swift) and opened it up on Xcode 8 beta 6.
It asked me to convert the code to Swift 3, which I did.
When I try to run the code on my phone I receive the errors:
Value of type ‘SCNNode’ has no member ‘run’
Value of type ‘SCNNode’ has no member ‘add’
Sample lines where the error occurs:
cameraYHandle.run(actionY)
self.cameraYHandle.add(cameraYAnimation, forKey: nil)
This leads me to three questions:
1) Are the functions 'run' and 'add' gone on SCNNode for Swift 3?
2) If so, what should I replace them with?
3) If so, if so, why didn't Xcode's converter handled them already?
Thank you for your time :)
PS.: It runned well for Mac using Xcode 7.3.
As dan commented, these translations resulted in a code without errors:
run => runAction
add => addAnimation
play => playAudio
so,
cameraYHandle.run(actionY) becomes cameraYHandle.runAction(actionY)
and so on.
Thank you, Dan.

StringByAppendingPathComponent() and WriteToFile()

I'm trying to learn Swift 3 with translating a code from Swift 2. In swift 2, I see the code like this:
return fullPath.stringByAppendingPathComponent(name)
But, when I try in Swift 3, I've got similiar code, but like this:
return fullPath.strings(byAppendingPaths: [name])
The issue is, return type in the 1st code is String (and that's the output I need from the lesson I learn), but return type in 2nd code should be [String].
The other issue is, in Swift 2 the code should be:
imgData?.WriteToFile(fullPath, atomicaly:Bool)
But in Swift 3, I only can input code like this:
imgData.Write(to: URL , option: WritingOption) throws
But in some examples, there's .Write(toFile: , atomically:) but I can't find it in Xcode.
Am I translating incorrectly or using both Swift 2 and Swift 3 incorrectly?
Regarding the first part of your question, as dan stated in the comments you should be using fullPath.appendingPathComponent(name) instead.
Regarding your second question:
The main difference between writeToFile and write(to: is the fact that the first is for Strings and the seconds is for NSData.
Somewhat related:
According to the NSData Class Reference
In iOS 2.0+ you have:
write(to:atomically:)
and
write(toFile:atomically:)
Given:
Since at present only file:// URLs are supported, there is no
difference between this method and writeToFile:atomically:, except for
the type of the first argument.
None of this has changed in Swift 3 according to the Swift Changelog.

ibeacon app development using swift

everyone, I am a new one to iOS app development using swift.
I am studying a ibeacon app sample code which downloaded from the https://github.com/SelimSalihovic/CityOS-iBeacon-Swift-Tutorial.
while I was running the code, there are errors in the code as shown the following page, could you help me how to solve it, please! Thanks in advance!
The first one is easily solveable by unwrapping the value (the exclamation mark)
NSUUID(UUIDString: "B9407F30-F5F8-466E-AFF9-25556B57FE6D")!
Second and third error are due to the beacons array not declaring the content's type (AnyObject means it can't be any class, which is not guaranteed to have the properties the code is looking for) so just go to line 16 and make the following change
var beacons : [CLBeacon] = []
However this will still not compile because the LocationServices framework hasn't been imported in the project, to do so just add
import CoreLocation
There will be some more errors now, specifically at line 26 and 55 in BeaconTableViewController
Fix-It has the right suggestion for these, basically you need to cast note.object by adding as! [CLBeacon] and remove the unwrapping on switch proximity because the value isn't optional
The code now compiles properly for me, I'm not sure it will work because I can't test right now, but it should be a step in the right direction
Good luck with your journey in iBeacons, they're a pretty fun technology to work with

Swift App Extension: instance method count is unavailable

I just create my first app extension using XCode 7.1. One code file containing the code below is shared with both targets:
var str = "";
var l = str.count; //Compile error for extension target App: count is unavailable: There is no ...
The reason for this compile error seams to be that App extension compiles with swift 1.2 while the container target compiles with swift 2.0.
One solution would be importing the content App into the extension App doesn't appear to be a good solution from what i read about it. Sharing the code between targets can be difficult if both are not compiled using the same compiler.
I just run through all target settings and didn't find nothing that could be changed.
Can't find any post about this problem, witch is not so uncommon, so it is must likely i am interpreting something in a wrong way.
The only solution i can think of is using NSString instead of String but that is just an workaround for one class type. More problems of this kind will emerge in the future.
In Swift 2 it's
str.characters.count
Use str.characters.count to get String length in Swift 2

Parse SDK and Swift: Incorrect argument label in call PFObject 'withoutDataWithObjectId'

I subclass PFObject exactly as described here.
Then I create a new instance of the subclassed object without data, but since Swift 1.2 I get an error (It did work perfectly before):
var test = Armor(withoutDataWithObjectId: "1234567890")
-> Xcode complains:
"Incorrect argument label in call (have 'withoutDataWithObjectId:',
expected: 'className:')"
Why className? It should get the class name from the class function parseClassName
And I can under no circumstances create a new object with objectId but no data (which I MUST have to fetch it from the local datastore)
This is super annoying as my app doesn't compile any longer.
Update to the newest Parse SDK, available here.
The issue is caused due to necessary adaptions in the Parse SDK after the Swift language update. This issue also occurs with the most recent update to Swift 2.2. The newest (as of today) Parse SDK release 1.13.0 already fixes this.
UPDATE
Parse iOS SDK 1.13.0 has a typo and the function PFUser(withoutDataWithObjectId:) is called PFUser(outDataWithObjectId:). So upgrading the Parse SDK alone does solve this. Until this is fixed a temporary workaround would be to extend PFObject with a convenience initializer. To do this add a new Swift file to your project and insert this:
import Parse
extension PFObject {
convenience init(withoutDataWithObjectId objectId: String?) {
self.init(outDataWithObjectId: objectId)
}
}
It may be a little late to answer this question.
I use swift 1.2, and v 1.7.5 Parse SDK, and it works totally fine.
however, make sure you have define objective-c bridging header in "build setting".
and try to run it, even though there may reports some error

Resources