I get a null return when i try out my NSString function.
//Track.m
static NSString* trackUrl;
//static NSString* getTrackNumberUrl;
@implementation Track
- (NSString*)trackUrl {
return @"http://site.com/?a=";
}
- (NSString*)setTrackNumberUrl:(NSString*)trackNumberUrl {
if (trackUrl != trackNumberUrl) {
return [trackUrl stringByAppendingFormat:trackNumberUrl];
}
return @"Error no trackNumber";
}
- (NSString*)getTrackNumberUrl:(NSString*)trackNumber {
return [[[self alloc] setTrackNumberUrl:trackNumber] autorelease];
}
@end
MainView.m, just to show the return answer in NSlog
- (NSString *) trackNumber{
return [track getTrackNumberUrl:@"86147224549XX"];
}
- (void)drawRect:(CGRect)rect {
NSLog(trackNumber);
}
I get a null return answer? Have i miss something? Thanks.
Edit some in Track.m
- (NSString*)setTrackNumberUrl:(NSString*)trackNumberUrl {
if (trackUrl != trackNumberUrl) {
return [trackUrl stringByAppendingString:trackNumberUrl];
}
return @"Error no trackNumber";
}
- (NSString*)getTrackNumberUrl:(NSString*)trackNumber {
return [[[Track alloc] setTrackNumberUrl:trackNumber] init];
}
This is how it should work.
getTrackNumberUrl –> setTrackNumberUrl –> trackUrl (return) –> setTrackNumberUrl + trackNumber –> getTrackNumberUrl (trackNumberUrl = trackUrl + trackNumber)
I have this code to set reference to Track
@class Track;
@interface MainView : UIView {
Track *track;
}
@property (nonatomic, retain) IBOutlet Track *track;
Well if don’t should use self alloc, what should i use?
You have a lot of problems with your code.
You should not use an arbitrary string as a format, because if it contains a format specifier like “%d” then the method will go looking for a variable that isn’t there, and will likely crash. You should use
stringByAppendingString:instead. However, that doesn’t seem to be what you want here, since the method name issetTrackNumberUrl:. If you want to change the value of thetrackUrlvariable, you can’t callstringByAppendingFormat:; all that does is return a new string and leave the original alone. I think you simply want something likeAnother problem:
In this context,
selfis an instance of Track. An instance won’t understand theallocmessage, that must be sent to a class. It will return a new instance, to which you should send aninitmessage. So you would do something like[[Track alloc] init].The first parameter to NSLog is a format string, so for the same reasons as above you shouldn’t use a variable, you should do something like this:
NSLog(@"%@", trackNumber);That line of code prints the value of the variable, trackNumber. Considering that you have a method namedtrackNumberjust above it, I wonder if what you really want to do is call the method and get the result. In that case, you need to write it as[self trackNumber]which will call the method and return an NSString.