财产申报中的含义atomic和nonatomic含义是什么?
@property(nonatomic, retain) UITextField *userName;
@property(atomic, retain) UITextField *userName;
@property(retain) UITextField *userName;
Run Code Online (Sandbox Code Playgroud)
这三者之间的运作区别是什么?
"非原子"在此代码中的含义是什么?
@property(nonatomic, retain) UITextField *theUsersName;
Run Code Online (Sandbox Code Playgroud)
原子和非原子有什么区别?
谢谢
自从开始研究iOS应用程序和目标C以来,我一直对可以声明和定义变量的不同位置感到困惑.一方面我们采用传统的C方法,另一方面我们有新的ObjectiveC指令,在其上添加OO.你能不能帮助我了解最佳实践和情况,我希望将这些位置用于我的变量,或者纠正我目前的理解?
这是一个示例类(.h和.m):
#import <Foundation/Foundation.h>
// 1) What do I declare here?
@interface SampleClass : NSObject
{
// 2) ivar declarations
// Pretty much never used?
}
// 3) class-specific method / property declarations
@end
Run Code Online (Sandbox Code Playgroud)
和
#import "SampleClass.h"
// 4) what goes here?
@interface SampleClass()
// 5) private interface, can define private methods and properties here
@end
@implementation SampleClass
{
// 6) define ivars
}
// 7) define methods and synthesize properties from both public and private
// interfaces
@end
Run Code Online (Sandbox Code Playgroud)
我用块做了好几次,就像我有强烈参考的指针一样
我听说你应该使用copy,但是使用块作为指针而不是原始对象的含义是什么?
我从来没有得到编译器的抱怨,我不应该使用
@property (nonatomic, strong) MyBlock block;
Run Code Online (Sandbox Code Playgroud)
但应该使用
@property (nonatomic, copy) MyBlock block;
Run Code Online (Sandbox Code Playgroud)
据我所知,块只是一个对象,所以为什么还要复制?
我的应用程序中有很多NSMutableString(差不多10-11); 全部定义为ivar/property
@property (nonatomic, retain) NSMutableString *str1;
Run Code Online (Sandbox Code Playgroud)
我在某处读到,最好对字符串使用"copy".真的吗?如果是,我可以在我的应用程序中替换retain复制并删除dealloc中的版本吗?
我还需要考虑其他一些事情吗?
此外,在1个应用程序中拥有10-11 NSMutableString是正常的吗?我的意思是从内存使用角度来看?我的应用程序中也有4-5个NSMutableDictionary.如果没问题,请告诉我.
我在iOS中比较新鲜,我想知道什么时候我们应该copy在一个属性中使用,例如
@property (nonatomic, retain) NSString* name;
Run Code Online (Sandbox Code Playgroud)
VS
@property (nonatomic, copy) NSString* name;`
Run Code Online (Sandbox Code Playgroud)
retain和之间有什么区别,什么copy时候应该使用一个而不是另一个?
考虑:
typedef void (^select_block_t)(UIView *) ;
(1) @property (copy, nonatomic) select_block_t myBlockProperty ;
(2) @property (strong, nonatomic) select_block_t myBlockProperty ;
(3) @property (assign, nonatomic) select_block_t myBlockProperty ;
Run Code Online (Sandbox Code Playgroud)
和:
(A) self.myBlockProperty = ^(UIView *) {NSLog(@"Hi");} ;
(B) self.myBlockProperty = [^(UIView *) {NSLog(@"Hi");} copy] ;
Run Code Online (Sandbox Code Playgroud)
我试图了解映射哪个属性声明与块复制语义的正确方法是什么
但后来我对"复制"操作多么冗余感到困惑.我有限的理解是[1:A]应该是正确的,因为我希望在分配属性时复制块一次,而不是在创建块时再复制一次,然后在属性赋值时再次复制.
根据我的理由,[3:B]也有意义.那么,我有什么误解?
什么是实例变量(在接口中声明的)工作的正确方法,他们@property和@synthesize在ARC项目时?我现在做的是:
SomeClass.h:
@interface SomeClass : NSObject {
NSString *someString;
}
@property(nonatomic, copy) NSString* someString;
Run Code Online (Sandbox Code Playgroud)
和SomeClass.m:
@implementation SomeClass
@synthesize someString;
- (void)someMethod {
self.someString = @"Foobar";
}
Run Code Online (Sandbox Code Playgroud)
问题是还有其他方法可行,比如只使用@property:
SomeClass.h:
@interface SomeClass : NSObject
@property(nonatomic, copy) NSString* someString;
Run Code Online (Sandbox Code Playgroud)
访问someString不包括self:
SomeClass.m:
@implementation SomeClass
@synthesize someString;
- (void)someMethod {
someString = @"Foobar";
}
Run Code Online (Sandbox Code Playgroud)
我是Objective-c的新手,我已经习惯了Java.那么使用属性的正确方法是什么?我知道特殊情况会有特殊行为,但一般来说最好的方法是什么?(一般来说,我的意思是我想从类本身和"外部"访问变量,我希望ARC仍能正常工作,例如.我不必担心内存泄漏)
我有以下启用ARC的代码
@property (nonatomic, weak) NSArray *a;
- (void)viewDidLoad
{
[super viewDidLoad];
self.a = @[@1, @2];
NSLog(@"ab is %@", self.a); //prints details of array
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
for (id element in self.a) { //empty here
NSLog(@"blah");
}
// Dispose of any resources that can be recreated.
}
Run Code Online (Sandbox Code Playgroud)
这是我用过的唯一的地方self.a.这是我编写的一个测试程序,用于调试我的一个问题.
当我模拟内存警告self.a消失?为什么?
在BNR iOS书中,作者说这样做copy而不是strong:
@property (nonatomic, copy) NSString *itemName;
Run Code Online (Sandbox Code Playgroud)
但我并没有真正理解这个目的,因为在main我尝试的方法中:
BNRItem *calculator = [[BNRItem alloc] init];
NSString *pickle = @"pickbarn";
backpack.itemName = pickle;
pickle = @"fuffle";
Run Code Online (Sandbox Code Playgroud)
当我打印出来backpack的名字到它的控制台picklebarn,所以我真的不明白,为什么itemName必须copied?
我是iphone开发的新手.任何人都可以告诉我什么时候使用强,什么时候使用弱参考,以及原子和非原子之间有什么区别.我经历了很多链接,但我仍然不清楚它.我还想知道ios5之前使用的保留,复制和分配之间的差异.
任何帮助表示赞赏..
谢谢,拉吉
当我在iOS中为应用程序创建属性时,何时应该使用"assign"?我什么时候应该使用"保留"?
一个人比另一个人有什么好处吗?
在myClass.h中,我有
@property (copy, atomic) NSMutableDictionary *thisParticularWordList;
Run Code Online (Sandbox Code Playgroud)
在myClass.m中,我填充thisParticularWordList,如下所示:
theSubview.thisParticularWordList = [NSMutableDictionary dictionaryWithDictionary: someDictionary];
Run Code Online (Sandbox Code Playgroud)
在xcode的变量视图中,我看到,该实例的属性确实已填充.
在我的代码的其他地方,我尝试这样做:
[self.thisParticularWordList removeObjectForKey:self.theKey];
Run Code Online (Sandbox Code Playgroud)
但不知何故,self.thisParticularWordList变成了一个不可变的NSDictionary.
我究竟做错了什么?
objective-c ×11
ios ×8
iphone ×5
cocoa ×3
properties ×2
atomic ×1
block ×1
cocoa-touch ×1
copy ×1
immutability ×1
ios5 ×1
nonatomic ×1
synthesize ×1