本文介绍了iOS ARC - 弱而强的属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图理解ARC的工作方式,据我所知,我应该在这里做错事。这是我正在使用的代码:

I'm trying to understand the way ARC works, and as far as I know, I should be doing something wrong here. This is the code I'm using:

接口:

@interface ViewController : UIViewController{

}

@property (strong, nonatomic) NSString * myString ;
@property (weak, nonatomic) NSString * myPointer ;

实施:

 - (void)viewDidLoad{

     [super viewDidLoad];
     self.myString = @"Hello world!" ; // myString is strong
     self.myPointer = self.myString ; // myPointer var is weak

     [self performSelector:@selector(makeNilMyValue) withObject:nil afterDelay:1];
     [self performSelector:@selector(printValues) withObject:nil afterDelay:2];
}

 - (void) makeNilMyValue{
     self.myString = nil ;
}

 - (void) printValues{
     NSLog(@"myString: %@", self.myString) ;
     NSLog(@"myPointer: %@", self.myPointer) ;
 }

执行此操作后,我得到:

After executing this, I get:

2012-02-26 11:40:41.652 test1[933:207] myString: (null)

2012-02-26 11:40:41.653 test1[933:207] myPointer: Hello world!

如果我没错,由于myPointer很弱,它不应该保留内容物体。所以,它应该显示为nil而不是Hello World!。

If I'm not wrong, due to myPointer is weak, it shouldn't retain the content of the object. So, it should show nil instead of "Hello World!".

我做错了什么?

根据Caleb的回答,我创建了另一个弱指针,请参阅下面的代码:

- (void)viewDidLoad{
    [super viewDidLoad];
    self.myString = @"Hello world!" ; // myString is strong
    self.myPointer = self.myString ; // myPointer var is weak
    self.myPointer2 = self.myString ; // myPointer2 var is weak

    [self performSelector:@selector(makeNilMyValue) withObject:nil afterDelay:1];
    [self performSelector:@selector(printValues) withObject:nil afterDelay:2];
}

- (void) makeNilMyValue{
    self.myPointer2 = @"value changed!" ;
    self.myString = nil ;

}

- (void) printValues{
    NSLog(@"myString: %@", self.myString) ;
    NSLog(@"myPointer: %@", self.myPointer) ;
}

重点是我仍然得到了我以前的答案:

The point is that I still got the same answer I used to have:

2012-02-26 12:08:13.426 test1[1333:207] myString: (null)
2012-02-26 12:08:13.427 test1[1333:207] myPointer: Hello world!


推荐答案

正如Caleb指出的那样,为此使用常量NSString示例不是一个好主意。

As Caleb pointed out, using a constant NSString for this example is not a good idea.

NSString * temp = @/ tmp / scratch;请注意,以这种方式创建字符串
常量时,应避免使用除7位
ASCII字符之外的任何内容。这样的对象是在编译时创建的,并且在程序执行期间存在
。编译器使这个对象
常量在每个模块的基础上是唯一的,并且它们永远不会被释放,
尽管你可以保留并释放它们,就像你做任何其他对象一样。你
也可以直接发送消息到一个字符串常量,因为你做任何
其他字符串:

NSString *temp = @"/tmp/scratch"; Note that, when creating a string constant in this fashion, you should avoid using anything but 7-bit ASCII characters. Such an object is created at compile time and exists throughout your program’s execution. The compiler makes such object constants unique on a per-module basis, and they’re never deallocated, though you can retain and release them as you do any other object. You can also send messages directly to a string constant as you do any other string:

BOOL相同= [@比较isEqualToString: myString];

BOOL same = [@"comparison" isEqualToString:myString];

解释了常量字符串永远不会消失。

The documentation explains that constant strings will never disappear.

尝试使用其他东西进行实验。我尝试了NSObject,它产生了预期的结果。

Try using something else for your experiment. I tried NSObject and it produced expected results.

接口:

@interface ViewController : UIViewController

@property (strong, nonatomic) NSObject * myString;
@property (weak, nonatomic) NSObject * myPointer;

@end

实施:

@implementation ViewController

@synthesize myString = _myString;
@synthesize myPointer = _myPointer;

- (void)viewDidLoad{

    [super viewDidLoad];

    self.myString = [[NSObject alloc] init];
    self.myPointer = self.myString;
    self.myString = nil;
    NSLog(@"myString: %@", self.myString);
    NSLog(@"myPointer: %@", self.myPointer);
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}

@end

弱指针设置为零如文档中所述,当没有强大的内存指针时 - 或。

Weak pointers are set to nil when there are no strong pointers to the memory as explained in the documentation - Apple Developer or llvm.

这篇关于iOS ARC - 弱而强的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-13 19:16