Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Jonse

macrumors newbie
Original poster
Dec 22, 2008
6
0
Hey you guys.

I'm new to OBjective C and Coca programming, but catching up well and liking the environment.

Today I encountered a somewhat strange problem though.
I was writing a simple function that returns a string:

Code:
-(NSString *) description
{
	return (@"Hello I'm a %@ with %d sides", [self name], [self numberOfSides]);
}

Building it gave the warning:
Warning: return makes pointer from integer without a cast

If fixed it by using:
Code:
-(NSString *) description
{
	return [NSString stringWithFormat:@"Hello I'm a %@ with %d sides %@", [self name], [self numberOfSides]];
}

But why didn't it work in the first place?

I'm quite curious in understanding the difference between:

Code:
return (@"Hello I'm a %@ with %d sides", [self name], [self numberOfSides]);
and
Code:
return [NSString stringWithFormat:@"Hello I'm a %@ with %d sides %@", [self name], [self numberOfSides]];

Thanks.
 

robbieduncan

Moderator emeritus
Jul 24, 2002
25,611
893
Harrogate
One tells the NSString class to to something. The other is just a lot of expressions in a set of brackets. This already has a meaning in C. You can't just invent syntax and hope it will work.
 

justfred

macrumors newbie
May 8, 2009
21
0
In your first example, there is absolutely no invocation of the NSString class methods. In the second example, you invoked the method defined in NSString.m, which takes your parameters, manipulates them, and allocates the string to memory. Your new string now inherits from the NSString class.

I believe the @"" syntax is just a shortcut, but I think I might be wrong.

You can do all these things manually, but not the way you attempted to do in your first example. As Robbie said, it just doesn't work.

In objective C, creating an instance of an object typically uses the methods contained in the object's class to do so.

Code:
// you use NSArray class methods to create an array too.
anArray = [NSArray arrayWithObjects:aDate, aValue, aString, nil];
 

BlackWolf

macrumors regular
Apr 9, 2009
244
0
well, @"" is basically a shortcut for creating an NSString, though that isn't really correct, but whatever.

but if you want to create a DYNAMIC string, so one where you put variables in, you cannot use @"", because @"" is not a method, it doesn't take any arguments, it can't be formatted. that can only be done via NSString's formatWithString method.
 

justfred

macrumors newbie
May 8, 2009
21
0
well, @"" is basically a shortcut for creating an NSString, though that isn't really correct, but whatever.

but if you want to create a DYNAMIC string, so one where you put variables in, you cannot use @"", because @"" is not a method, it doesn't take any arguments, it can't be formatted. that can only be done via NSString's formatWithString method.

oh yeah that makes more sense. It is not a shortcut to make an NSString... but rather it is a shortcut to making a string in objective-C. Thank you.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.