This will seem a dumb question, but as I've spent too much time doing ABAP, I'm afraid my C/Objective-C skills are pretty rusty.
I'm not sure if I'll ever produce any interesting app on the App Store, but anyways, I'm kind of having fun playing around with Xcode, Objective-C and the iPhone.
So here's the thing. I'm following Paul Hegarty's lessons on iTunes U and while trying to solve the exercises/enhancements to his programs, I came across the following conundrum.
In one class I have the following method:
In another class that calls the one above:
This works, result in cardSelected receives whatever is put in the string in doStuff.
However, when I first tried getting the result in cardSelected not as myGame return value but as an argument, result is always null. The code would be the following:
And the call looked something like this:
If the string is allocated in doStuff in both cases, why is not passed to the argument in cardSelected?
Thanks
I'm not sure if I'll ever produce any interesting app on the App Store, but anyways, I'm kind of having fun playing around with Xcode, Objective-C and the iPhone.
So here's the thing. I'm following Paul Hegarty's lessons on iTunes U and while trying to solve the exercises/enhancements to his programs, I came across the following conundrum.
In one class I have the following method:
Code:
-(NSString *)doStuff {
NSString *result;
result = nil;
...
if (...) {
...
result = [NSString stringWithFormat:@"You won %d points!", points];
} else {
result = [NSString stringWithFormat:@"You lost %d points!", points];
}
...
return result;
}
In another class that calls the one above:
Code:
@property (weak, nonatomic) IBOutlet UILabel *infoLabel;
- (IBAction)cardSelected:(UIButton *)sender {
NSString *result;
result = [self.myGame doStuff];
self.infoLabel.text = result; // or self.infoLabel.text = [self.myGame... ]; for short
[self refreshUI];
}
This works, result in cardSelected receives whatever is put in the string in doStuff.
However, when I first tried getting the result in cardSelected not as myGame return value but as an argument, result is always null. The code would be the following:
Code:
-(void)doStuff:(NSString *)result {
result = nil;
...
if (...) {
...
result = [NSString stringWithFormat:@"You won %d points!", points];
} else {
result = [NSString stringWithFormat:@"You lost %d points!", points];
}
...
}
And the call looked something like this:
Code:
@property (weak, nonatomic) IBOutlet UILabel *infoLabel;
- (IBAction)cardSelected:(UIButton *)sender {
NSString *result;
[self.myGame doStuff:result];
self.resultLabel.text = result;
[self refreshUI];
}
If the string is allocated in doStuff in both cases, why is not passed to the argument in cardSelected?
Thanks