Objective C 问题:方法不会返回字符串

发布于 2024-10-06 21:49:38 字数 785 浏览 3 评论 0原文

这是我的第一个问题,如果很明显,请原谅我。几年前我学会了用 Pascal 编程,所以我的术语可能不正确。我看了很多帖子,但似乎没有什么能解决我的基本问题。

我有一个查找表,用于将小数转换回分数。我正在调用这个方法...

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

...用这个..

[self convertToFractions:&float1 aString:outputFraction]; 

这个想法是 float2 是我传递给该方法的小数,而 myString 是返回的分数。

这在查找后运行:

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

日志显示 myString 是正确的分数,即 myString 正确显示了我想要返回的分数,但 outputFraction 是无效的。

我认为这是一个指针问题。我尝试了 *myString,但编译器抛出错误(类型不兼容)。

任何建议都非常感激。

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 terminology 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

暖心男生 2024-10-13 21:49:38

您想要将 convertToFractions 方法的输出从 void 更改为 NSString
它返回 null,因为您的方法的返回类型是 void,因此它什么也不返回。

Objective-C 方法的返回类型位于方法名称开头的括号中。

这是一个示例,但我不知道您在哪里定义 convertToString 因此,我将使用伪代码。

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

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

编辑:

正如其他人所建议的,您应该给 Objective-C 一个新的面貌。我建议您阅读 这篇 Objective-C 入门苹果。

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.

烏雲後面有陽光 2024-10-13 21:49:38

你在哪里定义你的outputFraction?上面的代码中没有任何地方提到它。

猜测您的转换方法被声明为 (void) 意味着它不会返回任何内容。如果您需要它以 NSString 形式返回结果,请声明它

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

到达方法末尾之前返回一个 NSString

return MyNSStringVariable;

,并确保在使用[EDIT]

我可以看到您希望您的方法返回 outputFraction 但事实并非如此Objective-C 的情况(不确定 Pascal)。您只需将 outputFraction 作为方法中的第二个变量传递即可。

因此,“正确”的做法是有一个方法

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

然后您可以将返回值分配给您选择的变量,例如:

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

干杯,
罗格

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

怪我鬧 2024-10-13 21:49:38

为什么不直接返回字符串呢?

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

顺便说一句:你真的需要阅读 ObjC。请不要尝试在 ObjC 上使用您旧的 pascal 知识。这是不同的,你的知识并不真正适用。

我建议购买一本关于 ObjC 的书或阅读一些很好的教程。 (苹果本身也有一些非常好的)

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)

極樂鬼 2024-10-13 21:49:38

如果您不想像其他人建议的那样从方法中返回 NSString,您可以将一个指向 NSString 的指针传递给您的函数(与将 NSError** 传递给某些标准 api 调用的方式相同,例如在 NSFileManager 方法中)。您的代码将类似于:

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

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

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]];
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文