目标 c iphone 整数

发布于 2024-08-19 20:53:53 字数 124 浏览 1 评论 0原文

是否可以说,在为 iphone 编程时使用整数时,您不会合成它们并且不会释放它们。

如果这是真的有人可以解释为什么吗?

每次我尝试时都会出错。 另外,如果我为整数值设置@property,我也会收到错误

Is it true to say that when using integers when programming for iphone that you do not synthesize them and that you do not release them.

If it is true could somebody explain why?

I get errors everytime that I try to.
Also if I set an @property for an integer value I too get errors

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

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

发布评论

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

评论(4

看轻我的陪伴 2024-08-26 20:53:53

在 Objective-C 中,int 是原始类型,而 NSNumber(或其他类似的东西)是对象。原始类型,因为它们不支持对象所做的许多事情(例如 getter 和 setter 方法),所以只能声明。

@property(assign) int x;

类似地,您不能释放它们,因为这样做意味着您正在发送 -向不支持消息传递的原语发布 消息。您可以@synthesize@dynamic它们,但前提是您给它们适当的(分配)属性。

简短的版本:像 int 这样的原语没有消息或方法。不要试图给他们一些。

In Objective-C, an int is a primitive type, whereas an NSNumber (or other similar things) are objects. Primitive types, since they don't support many of the things objects do (like getter and setter methods), can only be declared

@property(assign) int x;

Similarly, you don't release them, because doing so would imply you're sending a -release message to a primitive, which doesn't support messaging. You can @synthesize or @dynamic them, but only if you give them the appropriate (assign) property.

The short version: primitives like int don't have messages or methods. Don't try to give them some.

盛夏已如深秋| 2024-08-26 20:53:53

您可以合成int-s。但您必须将它们声明为 assign-properties:

 @property(assign) int X;

You can synthesize int-s. But you must declare them as assign-properties:

 @property(assign) int X;
风启觞 2024-08-26 20:53:53

两条评论:

  • NSNumber 比 int 慢得多。一般来说,如果您可以避免使用标量值,那么就这样做。

  • 定义属性时,尽可能使用“非原子”。它可以显着提高性能(当然,如果有多个线程对其进行操作,则存在一些风险)。

Two comments:

  • NSNumber is much slower than, say, int. In general, if you can get away with using scalar values then do so.

  • When defining properties, use 'nonatomic' wherever possible. It allows for a significant performance improvement (with some risks, of course, if you have more than one thread acting upon it).

夜吻♂芭芘 2024-08-26 20:53:53

如果您将整数声明为“NSInteger”或 C 风格的原始 int,那么您所说的是正确的。但如果您使用的是 NSNumber 类,则不然。让我解释一下:

NSInteger 不是 Objective-C 类。它只不过是整数的同义词。 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 是一个 Objective-C 类,是 NSValue 的子类。可以从有符号或无符号的 char、short int、int、long int、long long int、float、double 或 BOOL 创建 NSNumber 对象。
两者中,NSNumber只能用在集合中,比如NSArray,这里需要一个对象,而NSInteger则不能直接用在那里。

If you declare your integer as an 'NSInteger' or a C-style primitive int, then what you say is true. But not if you are using the NSNumber class. Let me explain:

NSInteger is not an Objective-C class.It is nothing more than a synonym for an integer. NSInteger is defined like this:

#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, and a subclass of NSValue. It is possible to create a NSNumber object from a signed or unsigned char, short int, int, long int, long long int, float, double or BOOL.
Among the two, NSNumber can only be used in collections, such as NSArray, where an object is required, but NSInteger cannot be used there directly.

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