Search code examples
iphonensstringnslog

NSString function


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?


Solution

  • You have a lot of problems with your code.

    return [trackUrl stringByAppendingFormat:trackNumberUrl];
    

    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 is setTrackNumberUrl:. If you want to change the value of the trackUrl variable, you can't call stringByAppendingFormat:; all that does is return a new string and leave the original alone. I think you simply want something like

    [trackUrl release];
    trackUrl = [trackNumberUrl retain];
    

    Another problem:

    return [[[self alloc] setTrackNumberUrl:trackNumber] autorelease];
    

    In this context, self is an instance of Track. An instance won't understand the alloc message, that must be sent to a class. It will return a new instance, to which you should send an init message. So you would do something like [[Track alloc] init].

    NSLog(trackNumber);
    

    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 named trackNumber just 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.