与 NSString 相关的问题

发布于 2024-08-18 13:07:43 字数 298 浏览 2 评论 0 原文

我有 1 NSString *abc = @"Hardik"; 我有 NSMutableArray *array; 现在我已经写了 [array addobject:abc];

然后我打印,NSLog(@"array = %@", array);

但我得到了 NULL 为什么? 我已经声明 NSMutableArray *array;在.ah文件中 我已经设置了 @property(nonatomic,retain)NSMutableArray *array; @合成数组;

我已经合成了它,但得到的值为 NULL 我无法理解吗?

I have 1 NSString *abc = @"Hardik";
i have NSMutableArray *array;
now i had written [array addobject:abc];

then i'm printing,NSLog(@"array = %@", array);

but i'm getting NULL
why?
I have declared NSMutableArray *array; in a.h file
i had set @property(nonatomic,retain)NSMutableArray *array;
@synthesize array;

and i have synthesize it but getting value NULL
I'm not able to understand it?

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

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

发布评论

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

评论(4

长发绾君心 2024-08-25 13:07:43

您还需要初始化数组:

array = [[NSMutableArray alloc] initWithCapacity:10];

这是非常基本的东西。您是否阅读过“学习 Objective C 入门”了吗?

You also need to initialise your array:

array = [[NSMutableArray alloc] initWithCapacity:10];

This is pretty fundamental stuff. Have you read the "Learning Objective C Primer" yet?

走过海棠暮 2024-08-25 13:07:43

听起来您实际上还没有分配数组。通常,您会在初始化程序中执行此操作。 (也不要忘记将 release 添加到您的 dealloc 方法中。)@synthesize 创建 getter 和 setter,但您仍然拥有自己处理对象的分配/解除分配。

It sounds like you haven't actually allocated array. Generally, you would do this in your initializer. (Don't forget to add a release to your dealloc method, too.) @synthesize creates the getter and setter, but you still have to handle allocating/deallocating the object yourself.

枕头说它不想醒 2024-08-25 13:07:43

听起来您的 NSMutableArray* 数组属性可能尚未初始化?

你能发布你的类初始化方法吗?

It sounds like your NSMutableArray* array property may not have been initialised?

Can you post your class init method?

夜巴黎 2024-08-25 13:07:43

要在类本身内触发合成访问器,您必须使用 self。如果不这样做,您将绕过访问器方法直接访问属性的地址。您需要:

NSString *abc = @"Hardik";
[self.array addobject:abc];
NSLog(@"array = %@", self.array);

这很重要的原因是合成方法通常也会初始化属性。合成数组方法的内部结构如下所示:

-(NSArray *) array{
    if (array!=nil) {
        return array;
    }
    array=[[NSMutableArray alloc] initWithCapacity:1];
    return array;
}

self.propertyName 实际上只是 [self propertyName]self.propertyName=someValue 的简写> 只是 [self setPropertyName:someValue] 的简写。

在您至少调用一次 self.array 之前,数组属性不会初始化

然而,只是为了混淆事情,一旦你在初始化后调用了 self.array ,那么你就可以直接调用 array 。所以...

[self.array addObject:abc];
NSLog(@"array = %@", array);

...有效,而相反只会返回一个空数组。

所以规则是:

  1. 在类实现中
    (包括子类),只调用
    propertyName 为您提供地址
    的属性,但不调用
    getter/setter 访问器方法。
  2. 在类内实现
    (包括子类),使用
    self.propertyName 调用
    getter/setter 访问器方法但是
    不直接访问属性。
  3. 从课外
    实施例如
    myClass.propertyName 调用
    getter/setter 访问器方法。

To trigger the synthesized accessor within a class itself, you must use self. If you don't, you access the attribute's address directly bypassing the accessor methods. You need:

NSString *abc = @"Hardik";
[self.array addobject:abc];
NSLog(@"array = %@", self.array);

The reason this is important is that the synthesized methods usually also initialize the property. The internals of the synthesize array method would look something like:

-(NSArray *) array{
    if (array!=nil) {
        return array;
    }
    array=[[NSMutableArray alloc] initWithCapacity:1];
    return array;
}

self.propertyName is really just shorthand for [self propertyName] and self.propertyName=someValue is just shorthand for [self setPropertyName:someValue].

Until you call self.array at least once, the array property is not initialized.

However, just to confuse things, once you have called self.array once it is initialized so you can just call array directly. So...

[self.array addObject:abc];
NSLog(@"array = %@", array);

...works while the converse would return just an empty array.

So the rules are:

  1. Within a class implementation
    (including subclasses), calling just
    propertyName gives you the address
    of the property but does not call
    the getter/setter accessor methods.
  2. Within a class implementation
    (including subclasses), using
    self.propertyName calls the
    getter/setter accessor methods but
    does not access attribute directly.
  3. From outside the class
    implementation e.g.
    myClass.propertyName calls the
    getter/setter accessor methods.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文