How to create an Array UIImage in Swift [duplicate] - swift

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.

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]!

Print contents of array with custom class swift [duplicate]

This question already has answers here:
Overriding description method in NSObject on swift
(1 answer)
How can I change the textual representation displayed for a type in Swift?
(7 answers)
Closed 5 years ago.
I have an array of type custom class as seen below. It has strings, ints, and double values.
class TrendData: NSObject {
var earlyTime = Date()
var recentTime = Date()
var earlyTimePrice = Double()
var recentTimePrice = Double()
}
I have an array as follows
let dataArray = [TrendData]()
I have filled in 2 values into the dataArray.
Right now when I use a print command as follows
print ("Label: \(dataArray)")
it prints this Label: [<App.TrendData: 0x600000472440>] ** [<App.TrendData: 0x600000472440>]
This is the correct behavior but I want to see all the values within each of these elements in the array. What is the best way to do that? I don't want to explicitly list out each element or even put it into a loop if I can avoid it. I am able to do it, it is just really messy right now. Is there any function or command that does this automatically?

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!)

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

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)
}
}