I have just started with Objective-C and have a (开发者_JAVA技巧probably) very, very basic question/problem.
int testf(int x){
NSDateFormatter *dateformatter=[[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"dd.MM.yyyy"];
NSString *infstr=[dateFormatter stringFromDate:[[NSDate] dateByAddingTimeInterval:(60*x)];
return infstr;
}
NSString *testString=testf(1);
I currently have the following problem: I don't know how to return a string from a function. I couldn't even figure it out through Google.
The aforementioned Code leads to a warning "Pointer from integer without a cast". I hope anyone does know a solution to this "problem" and is willing to share it with me.
Thanks in advance.
You need to create an NSString object using the int.
NSString *testf(int x) {
return [NSString stringWithFormat:@"%i",x];
}
NSString *testString = testf(1);
Just change your return type from int to an NSString pointer like so:
NSString* testf(int x){
NSDateFormatter *dateformatter=[[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"dd.MM.yyyy"];
NSString *infstr=[dateFormatter stringFromDate:[NSDate dateByAddingTimeInterval:(60*x)]];
[dateFormatter release];
return infstr;
}
NSString *testString=testf(1);
Also your dateFormatter is leaking, so add the release statement as shown above.
精彩评论