标签: nsnumber

Xcode调试:查看NSNumber的值?

是否可以在变量监视窗口中的调试器数据提示中看到NSNumber的数值?

我将一个Integer值存储在NSNumber中,并希望在调试期间看到此值.

我已经在调试器中尝试了一些数据格式化程序,但它没有多大帮助.

debugging xcode cocoa nsnumber data-formatters

1
推荐指数
1
解决办法
2168
查看次数

containsObject - 为什么这不起作用?

我有一个数组,我试图检查是否存在indexPath(.row).

我用这个代码:

if ([array containsObject:[NSNumber numberWithInt:indexPath.row]]){
    NSLog(@"Yep, it exists in there.");
}
Run Code Online (Sandbox Code Playgroud)

数组由数字3,8和2组成.索引路径在循环中加载从0到8的数字.

任何人都可以看到为什么这样做不起作用?

iphone nsnumber nsarray nsindexpath

1
推荐指数
1
解决办法
6264
查看次数

如何在Objective-C(Cocoa)中正确创建充满NSNumbers的静态NSArrays?

为什么在下面的代码中,我不能简单地创建一个NSNumbers的静态数组?我只会使用C数组和整数,但那些无法复制,正如你在init()中看到的那样,我必须将数组复制到另一个数组.我收到的错误是"初始化元素不是常数".这很混乱; 考虑到我没有任何地方的const关键字,我甚至不确定这意味着什么.

另外,作为旁注,getNextIngredient方法给出了错误"不能将对象用作方法的参数"和"返回不兼容的类型",但我不知道为什么.

这是代码:

// 1 = TOMATO
// 2 = LETTUCE
// 3 = CHEESE
// 4 = HAM

#import "Recipe.h"




@implementation Recipe

// List of hardcoded recipes
static NSArray *basicHam = [[NSArray alloc] initWithObjects:[[NSNumber alloc] numberwithInt:1], [[NSNumber alloc] numberwithInt:2], [[NSNumber alloc] numberWithInt:3], [[NSNumber alloc] numberwithInt:4]];

// Upon creation, check the name parameter that was passed in and set the current recipe to that particular array.
// Then, set nextIngredient to be the first ingredient of that recipe, so …
Run Code Online (Sandbox Code Playgroud)

objective-c nsnumber nsarray

1
推荐指数
1
解决办法
3519
查看次数

更新UILabel,从NSNumber获取价值

我正在制作学生预算申请表.当用户添加新费用时,代表所有费用总和的标签应更新,但不会.

float currentValue = [self.total floatValue];
self.total = [NSNumber numberWithFloat:(currentValue + amountValue)];
self.label.text = [NSString stringWithFormat:@"Razem: %@", total];
Run Code Online (Sandbox Code Playgroud)

在debuger 总计有一个适当的价值.

当然,当我输入这样的东西

self.label.text = [NSString stringWithFormat:@"something different"];
Run Code Online (Sandbox Code Playgroud)

标签改变了他的内容.

编辑.

我改了代码:

self.label.text = [NSString stringWithFormat:@"Razem: %@", [total stringValue]];
Run Code Online (Sandbox Code Playgroud)

但它只更新一次.我记录了这个: YT

iphone objective-c nsnumber uilabel

1
推荐指数
1
解决办法
4406
查看次数

将NSNumber转换为NSString并不是一个字符串

我有一个问题,将NSNumber值转换为NSString

MyPowerOnOrNot是一个NSNumber女巫只能返回1或0并且myStringNSString...

myString = [NSString stringWithFormat:@"%d", [myPowerOnOrNot stringValue]];

NSLog(@"%@",myString);
if(myString == @"1") {
    [tablearrayPOWERSTATUS addObject:[NSString stringWithFormat:@"%@",@"ON"]];
}
else if(myString == @"0") {
    [tablearrayPOWERSTATUS addObject:[NSString stringWithFormat:@"%@",@"OFF"]];
}
Run Code Online (Sandbox Code Playgroud)

这有什么问题?

NSLog节目0或控制台作为串1,但是我不能检查,如果它是1或0的if语句?

如果不实际应该跳到语句中......我真的不明白为什么这不起作用..任何帮助都会非常好!

objective-c nsnumber

1
推荐指数
1
解决办法
1万
查看次数

NSString错误 - 无法静态分配接口类型

我正在尝试创建一个NSDictionary对象,其中的键定义了歌曲的编号以及信息.我在尝试定义传入方法的NSNumber时遇到上述错误:

+(NSDictionary *) initWithMPMediaItem:(MPMediaItem *) song andSongNumber: (NSNumber *)songCount{

NSString *songKey = @"%@ Song Title", songCount;
Run Code Online (Sandbox Code Playgroud)

objective-c nsnumber nsstring

1
推荐指数
1
解决办法
3531
查看次数

NSNumberFormatter 不接受小数点后的零

类似的问题也有人提出过,但我发现所提供的解决方案不适用于最新的 iOS SDK,或者并不完全适用于这种特殊情况。

我使用 NSNotification 调用一个方法,该方法使用 UILabel 中的分组符号来格式化数字,因为它是使用按钮输入中的数字输入的。它也适用于整数和小数——只要小数点后不输入零。例如,如果输入 2000,则显示为 2,000,因为输入得很好。如果输入 23.345,也显示得很好。对这些数字所做的任何计算都是正确的。但不可能输入0.0001或0.2340007!它只是不会在小数点后添加任何零。

我从 stackoverflow 的另一篇文章中得到了原始代码的想法:

NSString *currentText = [display_ text];
currentText = [currentText stringByReplacingOccurrencesOfString:@","
               withString:@""];
currentText = [currentText stringByReplacingOccurrencesOfString:@"." 
               withString:@"."];

NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
[formatter setNumberStyle:NSNumberFormatterDecimalStyle];
[formatter setGroupingSeparator:@","];
[formatter setDecimalSeparator:@"."];
[formatter setMaximumFractionDigits:10];

NSDecimalNumber *number = [NSDecimalNumber decimalNumberWithString:currentText];

NSString *finalText = [formatter stringFromNumber:number];

NSString *lastChar = [currentText substringFromIndex:[currentText length] - 1]; 

if ([lastChar isEqualToString:@"."]) 
{
 finalText = [finalText stringByAppendingString:@"."];
}

[display_ setText: finalText];
[formatter release];
Run Code Online (Sandbox Code Playgroud)

对格式化程序的参数进行再多的欺骗(我已经尝试过)都不允许在小数点后输入零。

decimal-point nsnumber nsdecimalnumber nsnumberformatter ios

1
推荐指数
1
解决办法
2059
查看次数

从NSString获取一个char并转换为int

在C#中,我可以通过以下方式将我的字符串中的任何字符转换为整数

intS="123123";
int i = 3;
Convert.ToInt32( intS[i].ToString());
Run Code Online (Sandbox Code Playgroud)

Objective-C中此代码的最短等价物是什么?

我见过的最短的一行代码是

[NSNumber numberWithChar:[intS characterAtIndex:(i)]]
Run Code Online (Sandbox Code Playgroud)

objective-c nsnumber nsstring foundation

1
推荐指数
1
解决办法
9582
查看次数

在NSDictionary初始化中使用时,NSNumber变为nil

我有以下代码块:

NSNumber* dayNsNumber = [[NSNumber alloc] initWithInt:dayNumber+1];
NSLog(@"dayNsNumber: %d", [dayNsNumber intValue]);

if(boolVar){

    UIAlertView* success = [[UIAlertView alloc] initWithTitle:@"Title" message:[NSString stringWithFormat:@"Day %d!", dayNumber+1] delegate:self cancelButtonTitle:nil otherButtonTitles:@"Star", nil];
    [success show];
    NSLog(@"dayNsNumber: %d", [dayNsNumber intValue]);
    NSDictionary *params = [NSDictionary dictionaryWithObjectsAndKeys:
            dayNsNumber, "Day Number",
            [self getCurrentLocalDate], @"Date",
            [self getCurrentLocalTime], @"Time",
         nil];
}
Run Code Online (Sandbox Code Playgroud)

会发生什么事情,当我运行代码时,它会在初始化时挂起,params NSDictionary并说它dayNsNumber就是nil.

Xcode显示线程1:EXC_BAD_ACCESS(代码= 1).

我该如何解决这个问题?我想添加dayNsNumber到我的params字典中.

此外,这里是getCurrentLocalDategetCurrentLocalTime:

-(NSString*)getCurrentLocalDate;{ 
    NSString *formatString = [NSDateFormatter dateFormatFromTemplate:@"dd-MM-yyyy" options:0 locale:[NSLocale currentLocale]];
    NSDateFormatter *dateFormatter = …
Run Code Online (Sandbox Code Playgroud)

null objective-c nsdictionary nsnumber ios

1
推荐指数
1
解决办法
569
查看次数

为什么mutableCopy不能在NSNumber上使用?

码:

NSNumber* i = [[NSNumber alloc] initWithFloat:1.0];
NSNumber* j = [[NSNumber alloc] init];

j= [i mutableCopy];
Run Code Online (Sandbox Code Playgroud)

运行时j= [i mutableCopy],程序崩溃.

我知道NSNumber是不可改变的类型; 当您使用copy,i并且j将具有相同的存储器地址(参考副本).但是在使用时mutableCopy,它应该执行值复制.为什么mutableCopy不能用NSNumber

cocoa cocoa-touch objective-c nsnumber

1
推荐指数
1
解决办法
206
查看次数