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]!
Related
This question already has answers here:
Convert Swift string to array
(14 answers)
Closed 6 years ago.
I have a String and I want get an Array of the String's Characters.
I can already do it, like this:
var Carr = [Character]()
for c in s.characters {
Carr.append(c)
}
As you can see, it's not beautiful or efficient.
In Java, I can use char[] sa = s.toCharArray(); to get a char[]. Is there a similarly simple way to do this in Swift?
let charArray = Array(s.characters)
String.characters is a String.CharacterView. It conforms to BidirectionalCollection, which inherits from Collection, and ultimately Sequence.
Because it conforms to Sequence, it can be used in a for loop, as you showed. But also, it can be used in the initializer of Array that takes a sequence.
You are already using the right function:
let yourString = "a string"
let characters = yourString.characters
let count = characters.count
This gives you the collection of characters contained in your string
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!)
This question already has answers here:
Firebase - how to get the key value in observeEventType = Value
(3 answers)
Closed 6 years ago.
I'm using Firebase and getting a snapshot and when I print it I get this:
{
"-KOx03Q1f1Tl9AiWxNlg" = {
message = ".";
senderId = B6WI1xkEBXd4cwYkYFQRneEvPBV2;
};
}
I want to get the key, the value of -KOx03Q1f1Tl9AiWxNlg key. I can't figure out how to get it. I can print it as! NSDictionary and it looks exactly the same so it seems to be in that form...but I didn't know NSDictionary items had a key or name like that. Can anyone explain this?
You are using NSDictionary, so you can get array of all keys using allKeys property of NSDictionary
let keys = yourDic.allKeys as! [String]
In Swift 2.2 and later
let keys = myDictionary.keys as! [String]
Note: Access the first object of keys array to get -KOx03Q1f1Tl9AiWxNlg key.
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)
}
}
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.