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

thedon1

macrumors 6502a
Original poster
Jun 26, 2010
529
73
I've recently started learning Objective C and started making some basic things in Xcode.

It's been going pretty well the last couple of weeks but i'm stuck with something.

I know that NSString *myString would be making an instance of the object NSString called myString.

What does it mean when code is written as (NSString*) myString?

I don't get what the brackets are for and why the asterisk is inside the brackets. I just used NSString as an example but i've seen it with other things.

I've seen this in some example code but want to understand it before moving on.




It's probably a really basic question so i'm sorry but it's my first OO Language.


Thanks
 

animefx

macrumors regular
May 10, 2005
157
0
Illinois
In your example most likely something is being passed to a function or class.

so normally you might have something like:

Code:
myFunction(int x, int y)
{

}

...but when a function is expecting an instance, then it's written like:

Code:
myFunction(NSString *) myString
{

}

I'm not sure why it's not (NSString* myString) instead, but I think it could be and would work fine. Someone correct me if I'm wrong.
 
Last edited by a moderator:

chown33

Moderator
Staff member
Aug 9, 2009
10,706
8,346
A sea of green
...but when a function is expecting an instance, then it's written like:

myFunction(NSString *) myString
{

}

I'm not sure why it's not (NSString* myString) instead, but I think it could be and would work fine. Someone correct me if I'm wrong.

This is wrong.

A function expecting an NSString* looks like this:
Code:
myFunction(NSString * myString)
{

}
However, a method expecting an NSString* looks like this:
Code:
myMethod:(NSString *) myString
{

}

If you don't know the difference between a function and a method, you should review the fundamentals.

http://developer.apple.com/library/...ptual/OOP_ObjC/Introduction/Introduction.html
http://developer.apple.com/library/ios/#documentation/Cocoa/Conceptual/ObjectiveC/

----------

I know that NSString *myString would be making an instance of the object NSString called myString.

No it doesn't "make an instance". It declares a variable named myString.


What does it mean when code is written as (NSString*) myString?
You have to post the complete code, not just an isolated fragment.

I can think of at least two completely different cases:
Code:
NSString * aString = (NSString *) anotherObject;

someMethod: (NSString *) myString;
The first is a type cast. The second is a method declaration.

What book or tutorial are you learning from? Post the exact title, author, edition and/or URL.


I don't get what the brackets are for and why the asterisk is inside the brackets. I just used NSString as an example but i've seen it with other things.

I've seen this in some example code but want to understand it before moving on.
Exactly what code? Post a url.

And those are parentheses, not brackets.
 

thedon1

macrumors 6502a
Original poster
Jun 26, 2010
529
73
Thanks for the reply mate. Here's the code i'm using.

It's a method that basically get's the value from a slider I have and assigns it to a textfield.

Code:
-(IBAction)sliding:(id)sender;
{

    UISlider *s = (UISlider*) sender;
    
    int value = (int) s.value;
    NSString *newLabel = [[NSString alloc]initWithFormat:@"%i",value];
    sLabel.text = newLabel;
}

I understand it all apart from
Code:
UISlider *s = (UISlider*) sender;


Given your previous post, it looks like it's casting sender as a UISlider and then passing it to s.

this makes sense as sender is an id type.

Is this correct?
 

chown33

Moderator
Staff member
Aug 9, 2009
10,706
8,346
A sea of green
Correct. That use is a type cast.

However, it's not "passing" it to s. It's "assigning" it to s. Or one could say "storing it in s".

"Passing" usually refers to a value given as the argument (parameter) to a function or method. So value is passed to the initWithFormat: method, but "s.value" is assigned to the value variable in the line prior to that.
 

thedon1

macrumors 6502a
Original poster
Jun 26, 2010
529
73
Thanks for taking the time to reply, that really cleared that up.

One follow up question. If i didn't use the variable s, could I still get to the exact same result, just with a bit more code?

Could the first 2 lines of code be written in one (longer) line?
 

MattInOz

macrumors 68030
Jan 19, 2006
2,760
0
Sydney
Do you mean...
Would this still work?

First thing to ask is "if I experiment and it goes wrong what is the worst that will happen?"
The answer to me seems to be 9 times out of 10 is it'll crash. Just make sure you comment out your working code so you can revert if it doesn't work. The system seems to be pretty forgiving of exploration. I've haven't managed to kill anything other than the program I was trying to write. Believe me I've done some stuff that was really dumb in hindsight.

So read up on Type Casting plus also get an understanding of what an "id" type is, then decide if you think this will work and why.

Code:
-(IBAction)sliding:(id)sender;
{
    sLabel.text = [[NSString alloc]initWithFormat:@"%i",(int) [(UISlider *)sender value]];
}

Once you've come to a conclusion then type it in, see if the compiler is happy with it. If so see if it still produces the correct result when running.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.