last day (15 days later) » 

10:12 PM
0
Q: Display images in tableViewCell from an array of images

Mamoun MKI have an error message every time when a run this code below to display my image from an array on my cell table view. The error message: Could not cast value of type 'NSTaggedPointerString' (0x10b7ae860) to 'NSArray' (0x10b7ae900). var product = NSArray!() var imageProduct = ["AW":...

 
yes --> var product = NSArray!()
I put a breakpoint on it an it show me products -->NSString -->Anyobject with the value = "AW"
I changed but still the same problem ... I have this error in this line --> for key in products as! [AnyObject] {
 
The exception clearly tells you that the key of the products dictionary is a string (As is quite common) not an array
 
But Products is a string with the value "AW" when a put a break point on it products NSTaggedPointerString * "AW" 0xa000000000053552
 
You don't show where product is set but assuming it is something like imageProduct shown in your code, then the keys are strings (AW, AC) so force down casting a string to an array will fail, and you have the exception to prove it
If products is a string then the for loop makes no sense. Perhaps you could update your question showing the output of print(products)
 
Can you show some code with my example please ! I'm more visual :)
 
10:13 PM
I don't think you need the for loop at all if products is a string.
Certainly your code makes no sense because in the for loop you force downcast key to be [AnyObject] and then you force downcast it to a string in the next line. It can't be both
 
So how can I solve this problem
 
By understanding what your data is and what you are trying to achieve.
 
i have products and their images
I'm getting the product name from parse
 
You don't show where you load data into product - is it an array of strings? It looks like it from what you are saying
 
and I have all images in my app
I want just make a connexion between my array row
with the image I have already in my app
on my tableview cell
 
10:16 PM
Delete the for loop statement and just use products where you use key
 
oh
ok let try that
lets
 
Also, you are conditionally unwrapping image and then in the next line you check for nil - this is what you just did with the unwrapping. Image can never be nil at that point
 
I love you !
:D
it's working
var product = NSArray!()
var imageProduct = ["AW":UIImage(named: "MAC.png"),"AC":UIImage(named: "Windows.png")]

let initialThumbnail = UIImage(named: "ABC.png")
let products = product[indexPath.row]


if let image = self.imageProduct[products] {
if image == nil {
myCell.imageView?.image = initialThumbnail
}
else {
myCell.imageView?.image = image

}
}
that's ok like that ?
 
yes, I would probably use the conditional unwrap rather than the explicit check for nil; it is more "Swifty".
 
I'm new in swift
thanks
I don't know
what do you mean
by use the conditional unwrap
 
10:25 PM
if let image = self.imageProduct[products]
This checks for nil and assigns the constant if it isn't nil and then executes the clause. If it is nil then it executes the else.
 
Oh I see
that make sence
sense
thanks a lot for your help
 
You can use the nil coalescing operator ?? to make it even simpler:

myCell.imageView?.image = self.imageProduct[products] ?? initialThumbnail
 
oh hehe
what's that ?
this ??
I never saw it before
it's like if ?
 
It is called the nil coalescing operator. It says return the first value if it isn't nil otherwise return the value after ?? It is similar to the ternary operator ? in C
 
Oh I see Paul
So much to learn
some advice to give me to learn
 
10:30 PM
search through the Swift book for the nil coalescing operator.
 
more about swift ?
 
Have you read Apple's swift book? How many times?
 
to know how to write smarter
no
I just play with xcode and some tutorials
 
Ok. Well that is the starting point. It is in iBooks and it is free.
 
ok
and from that book
where do i have to go >
 
10:32 PM
Read it. Play around, go back and read it again and understand more...
 
?
I see !
u are right
 
I don't know. I don't really use Swift. It is getting better but I find it is still changing too much and debugging is too hard for me to use it in production code.
 
this one right
so what are u using in general
to create apps
 
yes
I use Objective C.
 
obj c?
it's better ?
 
10:35 PM
Swift has some nice features that in theory make it easier to write more robust code but it is still new (It only came out a little over a year ago). I have been using Objective C for 7 years and C for 20 years so I am comfortable with it
And people still abuse things in Swift like using ! when they don't know what it really means and end up with apps that crash. Just look at the number of questions on SO about "my app crashed with unexpectedly found nil...."
 
Yes that's true !
Do I have to stop learning swift
and start obj c ?
 
No. I think some familiarity with Objective C helps because there is a lot of sample code out there in that language since it has been around for a long time
 
I see
Thanks Paul
I have a last question
I want make a connexion with user and their profile picture
from parse
In tableview cell
but the problem is every time when I refresh the table view
all images disappear
I can show you my code
let userNamesOfRow = TopUsername[indexPath.row]

let userTable = PFQuery(className:"_User")

userTable.whereKey("username", equalTo: userNamesOfRow )
userTable.selectKeys(["username", "image"])

userTable.orderByAscending("username")

userTable.findObjectsInBackgroundWithBlock {(objects: [AnyObject]?, error: NSError?) -> Void in

if error == nil {

for object in objects!
{


let usersPicture = object["image"] as? PFFile



let initialThumbnail = UIImage(named: "ProfileImage_Fotor")

if usersPicture == nil {
 
11:05 PM
Once you have retrieved the picture you should store it in a property, not just in a local variable.
 
like using
NSUserDefault ?
NSData
I mean
 

last day (15 days later) »