Objective C Function Question
Hey guys, check this out. I have a functi开发者_如何学编程on that treats for me a string. No matter what it does, i just want to knwo if is possible to this function return the result for the place that it was executed. I mean, check this:
[self priceFormat:@"1"];
priceLabel.text = price;
-(void) priceFormat:(NSString*)price {
price = @"2";
}
I just want to my function treats the string and return it to the same place that it was executed.
Thanks!
Three ways to do this
Way one, using a pointer
- (void)priceFormat:(NSString **)price {
*price = @"2";
}
Wat two, using an instance variable
What you might want instead is an ivar. In the interface (most often the h
file) of your class:
NSString *price;
and in the implementation (the m
or mm
file):
- (void)priceFormat:(NSString *)price {
price = @"2";
}
I have created an example of this here.
If you want the price to be available to other objects as well (not just self
), you might want to create a property for it and synthesize it. Then use self.price = @"2";
instead. More on this here: http://MacDeveloperTips.com/objective-c/objective-c-properties-setters-and-dot-syntax.html
Just make sure you make it a copy
property (NSString in use)!
Way three using return
Note, that you can also return directly from a method:
- (NSString *)priceFormat:(NSString *)price {
return @"2";
}
priceLabel.text = [self priceFormat:@"1"];
精彩评论