StringByAppendingPathComponent() and WriteToFile() - ios

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.

Related

Issue with String initializer in Swift when using format parameter

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.

Swift compiler error: missing argument labels [duplicate]

This question already has answers here:
Swift 3 first parameter names
(5 answers)
Closed 5 years ago.
I run Swift on Xcode 8.3.3. Here I get an error regarding closures.
It seems that this part is all correct [var intro = introToFriends("Jim", "Pam")
intro] yet isn't clicking.
What's the issue?
Later Swift versions require explicit argument labels when calling a function. In your case it needs to read:
var intro = introToFriends(friendOne: "Jim", friendTwo: "Pam")
Alternatively, you can allow for omitting the use of argument labels if you add underscores to the parameter labels in the function declaration such as:
func introToFriends(_ friendOne: String, _ friendTwoString) {
...
}
For more details check the official Apple documentation on function argument labels.

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.

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

Swift 2 - Visualizing an AVMutableComposition for Debugging : Converting Apple's AVCompositionDebugViewer

I'm running into an issue with my swift 2 conversion of an Apple provided example for displaying an AVMutableComposition. This is a really useful project if you're trying to visualize your AVComposition to see what might be going on under the hood.
Update: I added print statements to each function to see the order they are being called, and who is calling them, in comparison to the Obj-C project.
Two Issues that I'm seeing that seem pretty important:
synchronizePlayerWithEditor() is not getting called after buildTransitionComposition(_:andVideoComposition:andAudioMix:)
observeValueForKeyPath(_:...) is NOT being called in the same order as the Obj-C project
Posting the snippet here to get the calling function as it's kind of useful
Obj-C
NSLog(#"%d %s %#", __LINE__, __PRETTY_FUNCTION__, [[NSThread callStackSymbols] objectAtIndex:1]);
Swift
func functionnYouWantToPrintCaller(yourNormalParameters..., function:String = __FUNCTION__){...}
print("\(__LINE__) \(__FUNCTION__) \(function)
Here is Apple's AVCompositionDebugViewer project I'm working from: https://developer.apple.com/library/mac/samplecode/AVCompositionDebugViewer
My github repo:
https://github.com/justinlevi/iOSAVCompositionDebugViewerSwift
I think the issue might be stemming from something in the keyValueObservation code although I'm not entirely sure at this point.
The issue ended up being in SimpleEditor.swiftbuildCompositionObjectsForPlayback` method. There were some global variables that were being defined incorrectly.
Everything seems to be working as expected now.
https://github.com/justinlevi/iOSAVCompositionDebugViewerSwift

Resources