I’ve been studying objective-c for a few days now. I keep coming across these two structures:
-
NSString * somestring
-
(NSString *) somestring
I understand the first simply sets a pointer to an NSString object, but what does the second construct do, and when should I use it?
What’s the deal with the asterix marks?
Sorry if this question doesn’t make any sense, I am completely new to this language, and haven’t even reached the level of asking proper questions.
Main purpose — I’m trying to decipher this method:
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger) row forComponent: (NSInteger)component
**Also, the classes I chose don’t matter. **
What you want to understand is the method declaration syntax. In Objective-C, a method has a name which looks like this
doSomethingWith:andAlso:inMode:. Each colon precedes an argument. When you declare or define a method, you also specify the types of the variables and their names; the type is given in parentheses. You also prepend things with a-for instance methods and a+for static methods, as well as the (parenthesized) return type. Now, you haveWhen we decipher this, we find that:
-: It’s an instance method.(NSString*): The return type,NSString*.pickerView:: The first part of the method name.(UIPickerView*)pickerView: The first argument; its name ispickerViewand it has typeUIPickerView*.titleForRow:: The second part of the method name.(NSInteger)row: The second argument; its name isrowand its type isNSInteger.forComponent:: The third part of the method name.(NSInteger)component: The third argument; its name iscomponentand its type isNSInteger.Thus, putting it all together, this defines the instance method
pickerView:titleForRow:forComponent:; it returns anNSString*, and the three arguments it takes are of typeUIPickerView*,NSInteger, andNSInteger, respectively. This method would be called as[obj pickerView:myPV titleForRow:myRow forComponent:myComponent].And just for further reference: in isolation, if you have
NSString* str, it declares a variable of typeNSString*; and if you have(NSString*)obj, it (forcefully) converts that object to have typeNSString*. This has no connection to the method declaration syntax.Edit 1: I also saw that you were asking about the asterisks. In (Objective-)C, this represents a pointer. If you have an
int x, say, then when you writeint y = xand theny = 3, the original value ofxis unchanged. If, however, you have anint* px, then you can writepx = &x. (Note that ints, declared asint, are a completely different data type than int pointers declared asint*. Writingint y = &xis garbage, as isint* py = x, and so on.) This&is the “address of” operator; it finds wherexis in memory and returns it. Now, if you writeint* py = pxand thenpy = &y, this won’t changepx. But if you write*px, you access the value currently stored inx, and you can change it:*px = 42setsxto42. For various reasons, when working with objects, people tend to like to work with references to them instead of their actual values; thus, in Objective-C, you only handle objects through pointers. What this means is that you will never seeNSMutableArray x, onlyNSMutableArray* x; and that if you haveNSMutableArray* y = x, thenxandyare, roughly speaking, the same, and calling[x addObject:obj]affectsyas well. There are more comprehensive tutorials out there—it’s worth checking them out if you don’t understand pointers—but this should suffice as an overview.Edit 2: In another comment, you say you’re coming from Ruby and Python. In Ruby (and I think Python, but I’ve used it less), every variable is a reference. This means that the basic use of pointers for object types should be familiar; as long as you never use
&or*, they’ll function in pretty much the same way. The difference between pointers and references is that you can take references to objects and create pointers to pointers. For instance, many methods end in...error:(NSDictionary**)error. This effectively provides an extra return value; in the method, if something goes wrong, they can write*error = myErrorInfo. Since function arguments are copied,error = myErrorInfowouldn’t be visible; however, the pointer’s referent is still the same, and so it can be assigned to. If you then write code such as:You pass in a pointer to
errorDictso that theunsafeOperation:error:method can write toerrorDictand you can see it.