objective-c 将 NSInteger 变量传递给 NSMutableDictionary 或 NSMutableArray

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1339394/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 22:07:45  来源:igfitidea点击:

Passing NSInteger variable to NSMutableDictionary or NSMutableArray

objective-ciphone

提问by 4thSpace

Why does this not work:

为什么这不起作用:

NSInteger temp = 20;
[userSettingsFromFile setObject:temp forKey:@"aTemp"];

but this does:

但这确实:

[userSettingsFromFile setObject:@"someObject" forKey:@"aTemp"];

How can I use the NSIntegervariable?

如何使用NSInteger变量?

回答by Matt Ball

NSIntegerisn't an object -- it's simply typecast to inton 32-bit or longon 64-bit. Since NSDictionarycan only store objects, you need to wrap the integer into an object before you can store it. Try this:

NSInteger不是对象——它只是类型转换为int32 位或long64 位。由于NSDictionary只能存储对象,所以需要将整数包装成对象才能存储。尝试这个:

NSInteger temp = 20;
[userSettingsFromFile setObject:[NSNumber numberWithInteger:temp] 
                         forKey:@"aTemp"]; 

回答by dreamlax

In order to store numbers in collections, you have to wrap them up in an NSNumberinstance.

为了将数字存储在集合中,您必须将它们包装在一个NSNumber实例中。

double aDouble = 20.3d;
NSInteger anInt = 20;

NSNumber *aWrappedDouble = [NSNumber numberWithDouble:aDouble];
NSNumber *aWrappedInt = [NSNumber numberWithInteger:anInt];

NSArray *anArray = [NSArray arrayWithObjects:aWrappedDouble, aWrappedInt, nil];

回答by ttvd

Whatever you pass through setObject has to be derived from NSObject. NSIntegeris not, it's a simple int typedef. In your 2nd example you use NSString, which is derived from NSObject.

无论您通过 setObject 传递什么,都必须从NSObject. NSInteger不是,它是一个简单的 int typedef。在您的第二个示例中,您使用的NSString是从NSObject.

回答by Durai Amuthan.H

NSInteger is synonym for long integer.What follows is how NSInteger is defined:

NSInteger 是 long integer 的同义词。以下是 NSInteger 的定义方式:

#if __LP64__ || NS_BUILD_32_LIKE_64
typedef long NSInteger;
typedef unsigned long NSUInteger;
#else
typedef int NSInteger;
typedef unsigned int NSUInteger;
#endif

NSNumber is an Objective-C class, a subclass of NSValue to be specific. You can create an NSNumber object from a signed or unsigned char, short int, int, long int, long long int, float, double or BOOL

NSNumber 是一个 Objective-C 类,具体来说是 NSValue 的子类。您可以从有符号或无符号字符、short int、int、long int、long long int、float、double 或 BOOL 创建一个 NSNumber 对象

One of the primary distinctions is that you can use NSNumber in collections, such as NSArray, where an object is required. For example, if you need to add a float into an NSArray, you would first need to create an NSNumber object from the float:

主要区别之一是您可以在需要对象的集合中使用 NSNumber,例如 NSArray。例如,如果您需要将一个浮点数添加到 NSArray 中,您首先需要从浮点数创建一个 NSNumber 对象:

float percentage = 40.5;

... // Create NSNumber object, which can now be inserted into an NSArray

... // 创建 NSNumber 对象,现在可以将其插入到 NSArray 中

NSNumber *percentageObject = [NSNumber numberWithFloat:percentage];

回答by Umit Kaya

Referred to @Matt Ball's answer: NSInteger isn't an object -- it's simply typecast to int on 32-bit or long on 64-bit. Since NSDictionary can only store objects, you need to wrap the integer into an object before you can store it.

参考@Matt Ball 的回答:NSInteger 不是一个对象——它只是类型转换为 32 位的 int 或 64 位的 long。由于 NSDictionary 只能存储对象,因此需要将整数包装成对象才能存储。

So, in case of Swift, this is how you do it:

所以,在Swift 的情况下,这就是你的做法:

let temp:NSInteger = yourInteger.hashValue

回答by PeyloW

Correction: Whatever is passed through setObject:do not have to be derived from the NSObjectclass, but it must conform to the NSObjectprotocol, that defines retainand release.

更正:通过的setObject:任何内容都不必从NSObject类派生,但它必须符合NSObject定义retain和的协议release

It can be confusing, but classes and protocols have different name spaces. And there is a both a class and a protocol named NSObject, the class NSObjectconforms to the protocol NSObject. There is one more root class, the NSProxyclass that also conforms to the NSObjectprotocol.

这可能令人困惑,但类和协议具有不同的名称空间。并且有一个类和一个名为的协议NSObject,该类NSObject符合协议NSObject。还有一个根类,NSProxy也符合NSObject协议的类。

This is important, because otherwise proxies could not be used in collections and auto release pools, while still having a lightweight proxy root class.

这很重要,因为否则代理无法在集合和自动释放池中使用,同时仍然具有轻量级代理根类。

回答by bob

just use: $(variable)syntax to convert primitive type to object.

只需使用:$(variable)语法将原始类型转换为对象。