Hello!
I know it is common problem but I read many posts and I still don't know what I'm doing wrong.
That is my code:
Code:
...
short val;
NSString *tmp;
val = [[value valueForKey:@"stock"] shortValue];
// everything works alright I checked using NSLog(@"%d", val). I have e.g. 10.
tmp = (@"%d\t", val);
And I have warning "Assignment makes pointer from integer without a cast", but if I start program and using this function it turns off.
I will be grateful if someone will help me
Just about how you should approach something like this (because at some point you'll be on your own):
The warning means exactly what it says: You are assigning a value. The value that you are assigning is an integer. The variable that you are assigning to is a pointer. That deserves a warning (actually, gcc is a bit stupid. This shouldn't be a warning, it should be a hard error).
Obviously that is not what you intended to do. But fact is that you are assigning an integer. The thing you are assigning is
So the compiler tells you it is an integer. Now what do you think this expression would mean? You have an Objective C string @"%d\t", followed by a comma, followed by an integer, and brackets around everything. We can ignore the brackets; what is inside the brackets is a so-called "comma-expression": Two expressions separated by a comma. In a comma-expression, the compiler evaluates the first expression, throws away the result, and then evaluates the the second expression, and that is the result. So you are really assigning
.
Where a comma expression is useful sometimes is something like
which sets x to 3*y + 1, then stores x*x into z (whether that is very readable code is debatable, but that is what the comma expression does). In your case it is clearly not what you wanted. You have to figure out what you actually wanted to do, and write the code that does it.
It takes a bit of practice to read warnings, but it is essential that you understand them. It is a good idea to have your code completely free of warnings so if any warning turns up after a change you notice it and fix the problem. What is very tempting and very very wrong is trying to "fix" the warning instead of the problem. In your case, you could write
Code:
tmp = (NSString *) (@"%d\t", val);
and the warning would be gone. However, the problem would still be there, except that you told the compiler not to warn you about it. (The compiler would take the integer "val", and pretend that it is a pointer to an NSString object instead of an integer. Results will be very, very ugly).
Just one thing that I am wondering about: Why do you artificially limit your code to values of "stock" from about -32000 to +32000? This doesn't serve any purpose at all.