Objective C 问题:方法不会返回字符串
这是我的第一个问题,如果很明显,请原谅我。几年前我学会了用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您想要将
convertToFractions
方法的输出从void
更改为NSString
。它返回 null,因为您的方法的返回类型是
void
,因此它什么也不返回。Objective-C 方法的返回类型位于方法名称开头的括号中。
这是一个示例,但我不知道您在哪里定义
convertToString
因此,我将使用伪代码。编辑:
正如其他人所建议的,您应该给 Objective-C 一个新的面貌。我建议您阅读 这篇 Objective-C 入门苹果。
You want to change the output of your
convertToFractions
method fromvoid
toNSString
.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.EDIT:
As others have suggested, you should give Objective-C a fresh look. I suggest you read this Objective-C Primer written by Apple.
你在哪里定义你的outputFraction?上面的代码中没有任何地方提到它。
猜测您的转换方法被声明为 (void) 意味着它不会返回任何内容。如果您需要它以 NSString 形式返回结果,请声明它
到达方法末尾之前返回一个 NSString
,并确保在使用[EDIT]
我可以看到您希望您的方法返回 outputFraction 但事实并非如此Objective-C 的情况(不确定 Pascal)。您只需将 outputFraction 作为方法中的第二个变量传递即可。
因此,“正确”的做法是有一个方法
然后您可以将返回值分配给您选择的变量,例如:
干杯,
罗格
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
And make sure you return an NSString before reaching the end of the method with
[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
Then you can assign the return value to a variable of your choice, for instance:
Cheers,
Rog
为什么不直接返回字符串呢?
顺便说一句:你真的需要阅读 ObjC。请不要尝试在 ObjC 上使用您旧的 pascal 知识。这是不同的,你的知识并不真正适用。
我建议购买一本关于 ObjC 的书或阅读一些很好的教程。 (苹果本身也有一些非常好的)
Why not just return the string?
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)
如果您不想像其他人建议的那样从方法中返回 NSString,您可以将一个指向 NSString 的指针传递给您的函数(与将 NSError** 传递给某些标准 api 调用的方式相同,例如在 NSFileManager 方法中)。您的代码将类似于:
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: