微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

objective-c – 在@Implementation下声明变量和在.m文件下声明@Interface之间的区别

我一直在学习Objective-C.根据我的学习,我知道当你在.h文件中的@interface中声明一个变量时,可以公开访问该变量(类似于 java中的公共变量).
@interface MyObject

@property NSInteger intData;

@end

但是当你在.m文件中的@interface中声明它时.它只能在@implementation下的.m文件中访问,除非你为它提供了一个getter和setter.

@interface MyObject ()

@property NSInteger intData;

@end

但我也注意到另一种声明变量的方法,它在@implementation下声明它

@implementation

NSInteger intData;

@end

我发现它的工作方式与在@interface下使用@property在.m文件中声明它的方式相同

我不明白两者之间的区别(在@implementation和@interface下的声明(在.m文件中).

我已经在堆栈中搜索了这个,但他们都在谈论@implementation和@interface(在.h文件中)之间的区别.所以认为这不是重复.

解决方法

首先,你没有声明一个变量;你是在申报财产.属性由实例变量支持,但它也添加方法.这里是放置变量的地方的解释:
@interface MyClass : NSObject {
    NSInteger i ;
}
@end

这是一个在您的类上放置实例变量的地方.它只能通过您的类和类别的方法访问. (旁注:可以在外部访问,但这不是推荐的做法)

一个例子:

@interface MyClass : NSObject
@end

@implementation MyClass {
    NSInteger i ;
}
@end

这也是一个实例变量,但只能通过该块内部编写的方法访问. (旁注:可以通过挖掘类定义来访问它,但这不是推荐的(或常见的)实践)

一个例子:

@interface MyClass : NSObject
@property NSInteger i ;
@end

是相同的:

@interface MyClass : NSObject {
    NSInteger _i ; // you are not allowed to access it by this variable
}
- (NSInteger) i ;
- (void) setI:(NSInteger)value ;
@end

这是一个允许人们获取和设置的财产.您可以在方法或其他方法中使用该变量:

NSLog ( @"The value is %i",self.i ) ; // if it's your instance method
NSLog ( @"The value is %i",object.i ) ; // if it's object's instance method

一个例子:

@interface MyClass : NSObject {
    NSInteger i ;
}
@property NSInteger i ;
@end
@implementation MyClass
@synthesize i ; // Causes the property to line up with the ivar by the same name.
@end

是相同的:

@interface MyClass : NSObject {
    NSInteger i ; // you ARE allowed to use this since you defined it
}
- (NSInteger) i ;
- (void) setI:(NSInteger)value ;
@end

在这里,您可以使用getter / setter方法或实例变量本身.但是,您通常应该使用这些方法,因为您[隐式]将它们声明为原子,因此它们具有线程同步.如果你想让它不做线程(并加快速度,只要你不打算在多线程环境中使用它):

@property (nonatomic) NSInteger i ;
@property (nonatomic,readonly) NSInteger i ; // only makes a getter method

我建议暂时避免这种情况并使用直接属性,因为它可以帮助您避免许多常见错误.除非您对程序进行概要分析并确定这是性能损失的原因,否则您应该只使用这些属性.

一个例子:

@interface MyClass : NSObject
@end

@implementation MyClass
NSInteger i ;
@end

这不是实例变量.它是一个全局变量,碰巧写在@implementation范围内.

请参阅上文,了解如何将其转换为实例变量(即将其置于大括号中).

还有一点说明:

声明这样的属性

@interface MyClass ()
@property NSInteger i ;
@end

不要把它私有化.但是,它隐藏在人们通常无法访问的文件中,因此编译器不知道属性存在.

代码中其他地方的其他功能仍然可以调用

[yourObject i] ;

要获得该属性的价值 – 但他们必须先了解它.

评论中回答问题的附录:

认情况下,属性是原子的.它不一定遵循原子的严格定义(这是一种蠕虫,我建议你现在不要看),但具有相同的效果:线程保证看到完整和最新的值,无论如何当另一个线程写入它时.它通常在合成getter / setter方法时执行此操作:

- (NSInteger) i {
    @synchronized(self) {
        return i ;
    }
}
- (void) setI:(NSInteger)value {
    @synchronized(self) {
        i = value ;
    }
}

如果您改为指定非原子,它将合成这些:

- (NSInteger) i {
    return i ;
}
- (void) setI:(NSInteger)value {
    i = value ;
}

如果您的财产是原子的,那么您不应该直接访问ivar.这样做会违反您开始时提供的线程保护. (旁注:有些情况你可以,但要等到你在尝试之前更熟悉线程/同步.)

原文地址:https://www.jb51.cc/c/111379.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐