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 + trackNumb开发者_如何学Pythoner --> 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.
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.
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.
return [trackUrl stringByAppendingFormat:trackNumberUrl];
I'm not sure bout this one, try using it as a format for string.
return [trackUrl stringByAppendingFormat:@"%@",trackNumberUrl];
精彩评论