What to use in place of stringByAppendingPathComponent in Swift 2 [duplicate] - swift

This question already has answers here:
stringByAppendingPathComponent is unavailable
(11 answers)
Closed 7 years ago.
I have just installed Xcode 7 with the new Swift 2, and I now have 50+ errors saying that "stringByAppendingPathComponent" is unavailable, and that I should use "URLByAppendingPathComponent" instead. I have been setting all of my texture properties like so:
let dropTexture = SKTexture(image: UIImage(
contentsOfFile:NSBundle.mainBundle().resourcePath!.stringByAppendingPathComponent(
"P04_rainDrop1.png"))!)
I have been doing this so they do not stay in memory when the SKScene is changed and it has been working perfectly. However directly replacing "URLByAppendingPathComponent" does not fix the errors.
How can I change this to fix the error and get the same SKTexture?

All you have to do is cast to NSString to recover stringByAppendingPathComponent, like this:
let dropTexture = SKTexture(image: UIImage(
contentsOfFile:(NSBundle.mainBundle().resourcePath! as NSString).stringByAppendingPathComponent(
"P04_rainDrop1.png"))!)
As Leo Dabus rightly says, you can save yourself from all that casting by adding an extension to String. However, you should not, as he suggests, call NSString(string:), which generates an extra string. Just cast:
extension String {
func stringByAppendingPathComponent(pathComponent: String) -> String {
return (self as NSString).stringByAppendingPathComponent(pathComponent)
}
}

Related

Extra argument in call when calling JSONSerialization.JSONObjectWithData [duplicate]

This question already has answers here:
Swift: Extra argument 'error' in call
(3 answers)
Closed 4 years ago.
let data = json.data(using: String.Encoding.utf8)!
let wrapper = JSONSerialization.JSONObjectWithData(data, options:nil, error:nil) as NSDictionary
Error is thrown on this line
let wrapper = JSONSerialization.JSONObjectWithData(data, options:nil, error:nil) as NSDictionary
what do i do?
Remove the extra argument. The Swift version of the method is declared like this:
class func jsonObject(with data: Data, options opt: JSONSerialization.ReadingOptions = []) throws -> Any
As you can see, it throws an error instead of taking an error parameter. There's even a sidebar in the documentation labeled Handling Errors in Swift: that explains a bit about how to use this method with try/catch to handle errors, and there's a link to more information on the topic.

the exact replacement for dictionaryWithObjects: forKeys: in swift [duplicate]

This question already has answers here:
Swift equivalent to `[NSDictionary initWithObjects: forKeys:]`
(5 answers)
Closed 5 years ago.
I am converting code from objective c to swift, I am unable to find the replacement for dictionaryWithObjects: forKeys: anything that I try is not giving the expected output.
I tried dict.updateValue(values[i]!, forKey: keys[i] as! String )
inside a for loop, but I realised that there are constant no of elements in keys(26) but for values(20,000) it has many elements.
So please help me!!
Sorry if I'm wrong, Thanks in Advance.
for i in 0..<alphabets.count {
in_memory_prediction.updateValue(prediction[i]!, forKey: alphabets[i] as! String)
}
You should be able to update dictionary directly even the value is object
var eachKey = alphabets[i] as! String
in_memory_prediction[eachKey] = prediction[i]!

Why can't I use .toInt() on a String in swift? [duplicate]

This question already has answers here:
.toInt() removed in Swift 2?
(5 answers)
Closed 6 years ago.
I'm making a mock remote control Iphone app using swift and Xcode is not letting me use .toInt() on the text of a UILabel. I'm trying to convert the text from a label into an integer and I'm not sure how to do this. Can anybody help me out? Thanks.
Here's my code:
#IBAction func channelInc(_ sender: UIButton) {
var chnl = channel.text!.toInt()
if (chnl!+1 > 99) {
} else {
let newChnl = chnl!+1
channel.text = "\(String(newChnl))"
}
}
There is not method on the String class called toInt(). The way to do it in swift is to use the initializer for the Int class.
let number = Int("5")
// returns a Int? (optional)
Try
let chnl:Int? = Int(channel.text!)

NSMapTable "Generic parameter 'KeyType' could not be inferred" [duplicate]

This question already has an answer here:
Declare "NSMapTable StrongObject" in Swift 3
(1 answer)
Closed 6 years ago.
The following line is giving me the compiler error "generic parameter 'KeyType' could not be inferred":
fileprivate var delegatesMap = NSMapTable.strongToWeakObjects()
I tried being more explicit by saying:
fileprivate var delegatesMap:MapTable<Key,Value> = NSMapTable.strongToWeakObjects()
But I then Xcode doesn't recognize "Key"
How do I go about fixing this?
EDIT: I would like my Key to be of type String, and my Value to be of type MenuActionDelegate(class protocol)
You may need to write something like this:
fileprivate var delegatesMap = NSMapTable<NSString, MenuActionDelegate>.strongToWeakObjects()
The generic parameters KeyType and ValueType need to be AnyObject, so you cannot directly put String there, also you need some explicit casting as NSString.
And the value type MenuActionDelegate needs to an #objc-protocol.
Or else you may need to write something like this:
var delegatesMap = NSMapTable<NSString, AnyObject>.strongToWeakObjects()
And use it as:
let theDelegate = delegatesMap.object(forKey: "delegateName" as NSString) as! MenuActionDelegate

How to create an Array UIImage in Swift [duplicate]

This question already has an answer here:
Expected Declaration error creating array in ViewController, can't work out why
(1 answer)
Closed 7 years ago.
This my code
var changePhoto = [UIImage]()
let photoPng = UIImage(named: "1.png")
let photoJpg = UIImage(named: "1.jpg")
changePhoto += photoPng
changePhoto += photoJpg
but error "expected declaration"
Please help me! What's wrong?
You need to use the append method to add new objects to your array:
changePhoto.append(photoPng)
changePhoto.append(photoJpg)
Also, you only can have functionality inside of methods. So try to call it inside a method. for example if you use a Viewcontroller in your viewDidLoad method.