从 UIPicker 中提取 NSString 后 NSString 转换为 doubleValue

发布于 2024-09-11 05:14:28 字数 607 浏览 3 评论 0原文

我有:

    NSInteger dollarrow = [ValuesPicker selectedRowInComponent:kDollar];

    NSString *dollar = [dollarlist objectAtIndex:dollarrow];

   double converteddollar=0;

我想对此进行 for 循环并获取 double 中的值,我正在尝试这样做:

    for(dollar=1;dollar<=99;dollar++)
    {
    converteddollar = converteddollar + 2;
    }

现在美元包含 NSString 我如何转换为 double 才能成功执行操作,然后

NSString *message = [[NSString alloc] initWithFormat:@"%0.2f",converteddollar];

我收到警告,并且应用程序是崩溃..我该如何纠正它。 我正在学习 Objective C 请帮忙。 谢谢 。 抱歉,我不想 == 我只是想使用 =

I have :

    NSInteger dollarrow = [ValuesPicker selectedRowInComponent:kDollar];

    NSString *dollar = [dollarlist objectAtIndex:dollarrow];

   double converteddollar=0;

I want to do a for loop on this and get the value in double , I am trying this :

    for(dollar=1;dollar<=99;dollar++)
    {
    converteddollar = converteddollar + 2;
    }

Now the dollar contains NSString how do I convert into double to do the operation successfully and then

NSString *message = [[NSString alloc] initWithFormat:@"%0.2f",converteddollar];

I am getting warnings and the app is crashing .. How can I correct it .
I am learning Objective C please help .
Thankyou .
Sorry I did not want == i just wanted to use =

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

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

发布评论

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

评论(3

盛夏已如深秋| 2024-09-18 05:14:28

你的 for 循环是错误的。你真的想要'=='吗?我猜你只想要“=”。你不能使用这样的东西吗? (我只是举一个例子来指出循环中的错误。您的要求可能有所不同。而且我还没有编译代码,现在没有mac。所以您可能会遇到一些小错误)

double sum = 0

for (NSInteger i = 1; i <= 99; i++) {
    sum += 2;  // or whatever you need here
}

NSString *convertedStr = [NSString stringWithFormat:@"%lf", sum];

Your for loop is wrong. Are you really want '=='? I guess you want only '='. Can't you use something like this? (I'm just giving an example to point the mistakes in loop. Your requirement may be something different. And also I have not compiled the code, don't have mac with me right now. So you may get some minor mistake)

double sum = 0

for (NSInteger i = 1; i <= 99; i++) {
    sum += 2;  // or whatever you need here
}

NSString *convertedStr = [NSString stringWithFormat:@"%lf", sum];
枯寂 2024-09-18 05:14:28

很多错误...

NSInteger dollarrow = [ValuesPicker selectedRowInComponent:kDollar];

selectedRowInComponent: 方法是一个成员方法。不是静态的。您需要在实例上调用它。

== 是比较运算符。您想要的当然是一个带有单个 = 符号的赋值。

您声明一个指向 NSString 对象的指针。但它尚未初始化。
您还应该在循环中使用临时双变量。

我不明白你的循环应该做什么。你能解释一下吗?

Lots of errors...

NSInteger dollarrow = [ValuesPicker selectedRowInComponent:kDollar];

The selectedRowInComponent: method is a member method. Not a static one. You need to call it on an instance.

== is a comparison operator. What you want is certainly an assignment, with a single =sign.

You declare a pointer to a NSString object. But it's not initialized.
You should also use a temporary double variable for your loop.

I don't understand what your loop is supposed to do. Can you explain a little bit?

撩人痒 2024-09-18 05:14:28

NSString *dollar = [dollarlist objectAtIndex:dollarrow];

double conversiondollar = [dollar doubleValue]; 应该可以工作(不需要 for 循环)

NSString *dollar = [dollarlist objectAtIndex:dollarrow];

double converteddollar = [dollar doubleValue]; should work (no need for you for loop)

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