views:

230

answers:

4
+1  Q: 

NSString function

Hello

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)

A: 

Most probably track is nil in the trackNumber - have you set it to a correct reference to a Track object?

Also, this code

- (NSString*)getTrackNumberUrl:(NSString*)trackNumber {
    return [[[self alloc] setTrackNumberUrl:trackNumber] autorelease];
}

is incorrect. Why are you using [self alloc]? You're allocating a new Track object (using a static method on an object reference, not on a class name, which is an error), setting it's track number URL, and returning an autoreleased NSString, but you're leaking the Track object you allocated.

Adam Woś
A: 

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?

Frozzare
Edit your question, or write a comment, but don't use the Answer box to post things that are not answers.
benzado
You can't post code in comment box!
Frozzare
Then edit your question.
Adam Woś
+2  A: 

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.

benzado
"You should initialize trackUrl to nil." Static variables are automatically initialized to zero.
KennyTM
I want to call the getTrackNumberUrl function in Track. The getTrackNumberUrl collect information from setTrackNumberUrl how are getting the base url from trackUrl.
Frozzare
I have fix that you write, but it still return null
Frozzare
Great explanation. I think this code is a little hopeless... BUt please remove the comment about initialization of static variable - it's incorrect, as KennyTM pointed out.
Adam Woś
@KennyTM: Thanks, I didn't know where I learned to be so defensive.
benzado
Yes it was greate explanation. But it still return null value.
Frozzare
A: 
return [trackUrl stringByAppendingFormat:trackNumberUrl];

I'm not sure bout this one, try using it as a format for string.

 return [trackUrl stringByAppendingFormat:@"%@",trackNumberUrl];
Nithin