Getting the first 3 letters from string in cell? - swift

I am trying to get the first 3 letters from this string but I am getting this error. Where do I go from here?
ERROR Ambiguous reference to member 'subscript'
let start = cell.viewWithTag(7) as! UILabel
start.text = posts[indexPath.row].start.characters.prefix(3)

Related

Variable used within its own initial value Swift 3

I try to convert my code to swift 3 an I have spent hours on the following error:
Type 'Any' has no subscript members
Here's was my original code:
let data: AnyObject = user.object(forKey: "profilePicture")![0]
I looked at the answers here but I'm still stuck. (I do programming as a hobby, I'm not a pro :/)
I've try that:
let object = object.object(forKey: "profilePicture") as? NSDictionary
let data: AnyObject = object![0] as AnyObject
But now I get this error:
Variable used within its own initial value
Second issue: Use always a different variable name as the method name, basically use more descriptive names than object anyway.
First issue: Tell the compiler the type of the value for profilePicture, apparently an array.
if let profilePictures = user["profilePicture"] as? [[String:Any]], !profilePictures.isEmpty {
let data = profilePictures[0]
}
However, the array might contain Data objects, if so use
if let profilePictures = user["profilePicture"] as? [Data], !profilePictures.isEmpty {
let data = profilePictures[0]
}
Or – what the key implies – the value for profilePicture is a single object, who knows (but you ...)
And finally, as always, don't use NSArray / NSDictionary in Swift.

Subscription error that cannot subscript value of type

I am converting my project from Swift 2 to Swift 3 and I am stuck with one weird error:
Cannot subscript a value of type 'inout [[String : AnyObject]]' (aka
'inout Array>')
What does the "inout" even mean in this error?
This is the code part:
var productsValue = [[String:AnyObject]]()
cell.snusProductImageView.kf.setImage(with: NSURL(string: productsValue[indexPath.row]["productUrl"] as! String)!)
What is causing this? I don't understand the error at all since it is so board. Can you guys help me?
I tried using guard and let but it doesn't work for some reason.
I understand that productsValue is a [String:AnyObject]. That means that["productUrl"] is an AnyObject and I can't assign an AnyObject where a [String:AnyObject] is expected. But why it worked in Swift 2?
I solved the error like this:
var productsValue = [[String:AnyObject]]()
if productsValue[indexPath.row]["productUrl"] != nil {
var productValue = self.productsValue[indexPath.row]["productUrl"] as! String
cell.snusProductImageView.kf.setImage(with: NSURL(string: productValue) as! Resource?)
}
I don't know if this fixes it and why does it silence the error but yeah.

Value type of "Any" has no member 'objectforKey' Swift 3 conversion

I am currently facing this problem (Value type of "Any" has no member 'objectforKey') due to swift 3 upgrade. Anybody knows why?
Here is my line of code that have the error
let bookName:String = (accounts[indexPath.row] as AnyObject).objectForKey("bookName") as! String
*accounts is an array.
Okay basically it is the .objectForKey needs to be change as the following:
let bookName:String = (accounts[indexPath.row] as AnyObject).object(forKey:"bookName") as! String
As always, do not use NS(Mutable)Array / NS(Mutable)Dictionary in Swift. Both types lack type information and return Any which is the most unspecified type in Swift.
Declare accounts as Swift Array
var accounts = [[String:Any]]()
Then you can write
let bookName = accounts[indexPath.row]["bookName"] as! String
Another Dont: Do not annotate types that the compiler can infer.

How do I add text from an array to a label

I am trying to create a very simple app as I am new to swift. I am trying to make an app where there are four textfields, someone can put in words in each of the text fields and it picks one of them at random. However I am getting the error: 'Cannot assign value of type 'Int' to type 'String?'.
As Leif answered, "(\randomIndex)" will let you print the index you generated. If you want to print the actual text for the text field though...
let randomTextField = array[randomIndex]
label.text = randomTextField.text
let randomIndex = arc4random_uniform(4)
label.text = "\(yourArrayOfStrings[randomIndex])"
Either put the Int value as "\(index)" or String(index)

Swift Error: Ambiguous reference to member 'subscript'

I'm new to coding and picked up some open source project to get the idea.
I'm getting the error:
Ambiguous reference to member 'subscript'
in the code below:
let pictures = ( selectedRestaurant["Pictures"] as! NSArray ) // Error
let picture = ( pictures[zoomedPhotoIndex] as! NSDictionary )
let pictureURL = picture["url"] as! String
let imageURL = NSURL(string: pictureURL)
let urlRequest = NSURLRequest(URL: imageURL!)
NSURLConnection.sendAsynchronousRequest(urlRequest, queue: NSOperationQueue.mainQueue()) {
response, data, error in
if error == nil && data != nil {
self.imageView.image = UIImage(data: data!)
self.imageView.contentMode = UIViewContentMode.ScaleAspectFit
}
}
Just specify explicitly what is the type of pictures:
So instead of:
let pictures = selectedRestaurant["Pictures"] as! NSArray
Write:
let pictures: NSArray = selectedRestaurant["Pictures"] as! NSArray
For me the answer was to specifically state the type of array I was casting to:
if let foo = dictionary["bar"] as? [String]
It means that "Pictures" is not a valid subscript. It looks like you are creating a constant named pictures and you are trying to assign it a value of selectedRestaraunt["Pictures"] and then trying to cast it as an NSArray. If selectedrestaraunt is already an array, then what goes in the [] brackets after selectedRestaraunt should be an integer value which will refer to an index in the selectedRestaraunt array. Obviosuly "Pictures" is not an integer, it is a string.
If you are trying to access an array within an array. Meaning that Pictures is an array stored within the selectedRestarauntarray then you can access it by using selectedRestaraunt[index of Pictures array] where [index of pictures array] is an integer which is equal to the index number in which the Picutres array resides within the selectedRestaraunt array
I managed to get this error in a somewhat weird way. I had code like this:
cell.textLabel = anArrayOfStrings[indexPath.item].uppercased()
And I was stumped as to why it couldn't figure out that this was an array, even though I very clearly declared its type. I broke the line in two and finally got a helpful error message:
let name = anArrayOfStrings[indexPath.item].uppercased()
cell.textLabel = name
I was trying to assign a String to a UILabel, but somehow the point at which the type inference engine failed was at the subscript.
So my advice to anyone stumped by this is to try to break up your statement into bite-sized chunks that the Swift type inference engine can more easily digest.
As Eric and Eugene mentioned in their comments it is impossible to review the issue you are having without knowing the selectedRestaurant type. That is after all why you are getting the compiler ambiguity error.
I have to respectfully disagree with MikeG though. The problem is not one of a valid subscript. You'd be getting that kind of error, if for example you had a selectedRestaurant type of [NSNumber:AnyObject], where clearly String is no longer valid since the dictionary key could only be an NSNumber.