加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

objective-c – 在@Implementation下声明变量和在.m文件下声明@I

发布时间:2020-12-16 03:27:00 所属栏目:百科 来源:网络整理
导读:我一直在学习Objective-C.根据我的学习,我知道当你在.h文件中的@interface中声明一个变量时,可以公开访问该变量(类似于 java中的公共变量). @interface MyObject@property NSInteger intData;@end 但是当你在.m文件中的@interface中声明它时.它只能在@implem
我一直在学习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.这样做会违反您开始时提供的线程保护. (旁注:有些情况你可以,但要等到你在尝试之前更熟悉线程/同步.)

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读