在目标 c 中调用方法 [英] calling a method in objective c

查看:22
本文介绍了在目标 c 中调用方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试学习目标 c.我有 xcode 4,正在阅读一本关于如何制作 iphone 应用程序的初学者书籍.

I'm trying to learn objective c. I have xcode 4 and am reading my way through a beginners book on how to make iphone applications.

我可以对 c 进行合理的编码,但 c 显然较低,并且涉及相当多的内存管理,所以我认为这就是我在这里失败的地方.

I can code c sharp reasonable well but c is obviously lower level and involves a fair bit of memory management so I think that's where i'm falling down here.

无论如何,我正在尝试创建一个返回类型为 double 的方法.在 c 锐利我会把它写成

Anyway, I am trying to create a method of return type double. In c sharp I would write this as

public double test()
{
    return 12;
}

然后我希望能够从另一个方法调用我的方法.这是迄今为止我一直在做的部分工作.

I then want to be able to call my method from another method. Here's part of what I've been working on so far.

- (double)test
{
    return 1985;
}

- (IBAction)btnConvert_Click:(id)sender
{
    double str = [double test];

    NSString *hello = txtAmount.text;
    double myDouble = [hello doubleValue] * exrate;
    lblResult.text = [NSString stringWithFormat: @"%.2f", str];
}

它在我尝试建立双方法 str预期表达式"时引发错误.我想做的是传入 myDouble 进行测试,然后进行一些计算,然后返回值.

It throws up an error where I try to establish the double method str "expected expression". What I'd like to do is to pass in myDouble to test, then to do some calculations and then return the value.

我该怎么做?谢谢.

推荐答案

代替 double str = [double test]; 你应该写 double str = [self test];code> 因为您正在调用您自己的类的方法,而不是 double

Instead of double str = [double test]; you should write double str = [self test]; since you are calling a method of your own class, not on double

这篇关于在目标 c 中调用方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆