开发者

Objective C question: method won't return a string

This is my first question so please forgive me if it's obvious. I learned to program in Pascal a few years ago, so my 开发者_如何学Cterminology may be off. I've looked at a bunch of postings, but nothing seems to address my basic problem.

I have a lookup table that I use to convert decimals back into fractions. I am calling this method...

-(void) convertToFractions:(float *)float2 aString:(NSMutableString *) myString;

...with this..

[self convertToFractions:&float1 aString:outputFraction]; 

The idea is that float2 is the decimal that I pass to the method, and myString is the fraction returning.

This runs after the lookup:

myString = [NSString stringWithString:[[decimalInchArray objectAtIndex:x]objectAtIndex:1]];
NSLog(@"myString = %@",myString);

The log shows myString is the correct fraction i.e. myString is correctly displaying the fraction I want to return, but outputFraction is null.

I think it's a pointer issue. I tried *myString, but the compiler throws an error (incompatible types).

Any suggestions are really appreciated.


You want to change the output of your convertToFractions method from void to NSString. It's returning null because the return type of your method, is void, so it returns nothing.

The return type of an Objective-C method is in the parenthesis, at the beginning of the method name.

Here,s an example, but I don't see where you define convertToString so, I'll use pseudocode.

- (NSString *) convertToFractions:(float *)float2{
  NSString *fraction = *some code to lookup fraction from table;*
  return fraction;
}

myString = [self convertToFractions:(float *)float2];

EDIT:

As others have suggested, you should give Objective-C a fresh look. I suggest you read this Objective-C Primer written by Apple.


Where do you define your outputFraction? Nowhere in the code above you mention it.

At a guess your conversion method is declared as (void) meaning it will not return anything. If you need it to return the result as a NSString declare it like

-(NSString*) convertToFractions:(float *)float2 aString:(NSMutableString *) myString;

And make sure you return an NSString before reaching the end of the method with

return MyNSStringVariable;

[EDIT]

I can see you are hoping that outputFraction will be returned by your method but that is not the case with Objective-C (not sure about Pascal). You are simply passing outputFraction as a second variable in your method.

So the "right" way of doing it would be to have a method

-(NSString*)convertToFraction:(float*)float2 {
     ...// Do your float to fraction calculation here
     ...// Make sure fraction is formatted as NSString
     return YourFractionVariable;
}

Then you can assign the return value to a variable of your choice, for instance:

NSString *fraction = [self converToFraction:aFloatNumber];
NSLog (@"fraction is %@", fraction);

Cheers, Rog


Why not just return the string?

- (NSString*)convertToFractions:(float)float {
    //Bla bla do everything
    return [myString autorelease]; //Autorelease is for memory-management
}

Btw: You seriously need to read into ObjC. Please don't try to use your old pascal-knowledge on ObjC. It's different, and your knowledge isn't really applicable.

I would recommend buying a book about ObjC or reading some good tutorials for it. (Apple itself has some very good ones)


If you don't want to return NSString from your method as others suggested you can pass a pointer to NSString pointer to your function (the same way you pass NSError** to some standard api callsm e.g. in NSFileManager methods). Your code will look something like:

NSString *outputFraction;
[self convertToFractions:&float1 aString:&outputFraction];

-(void) convertToFractions:(float *)float2 aString:(NSMutableString **) myString{
  ...
   if (myString)
      *myString = [NSString stringWithString:[[decimalInchArray objectAtIndex:x]objectAtIndex:1]];
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜