正如苹果的文档中指出:http://developer.apple.com/library/mac/#documentation/cocoa/Conceptual/ObjectiveC/Chapters/ocCategories.html#//apple_ref/doc/uid/TP30001163-CH20-SW1如何使用Objective-C中的类别访问@private实例变量?
Note that a category can’t declare additional instance variables for the class; it includes only methods. However, all instance variables within the scope of the class are also within the scope of the category. That includes all instance variables declared by the class, even ones declared @private.
然而,当我试图访问的UITextField“_selectionRange”的私有实例变量,我得到找不到符号错误。以下是我的源代码和错误消息供您参考。对于那些读过我最后一个例子“NSString”的人,我表示歉意。这不是一个好例子,因为在NSString类中没有任何@private实例变量。
的NSString + Utilities.h
#import <Foundation/Foundation.h>
@interface UITextField (Editing)
- (void)deleteBkw;
@end
的NSString + Utilities.m
@implementation UITextField (Editing)
- (void)deleteBkw {
NSLog(@"%d:%d", _selectionRange.location, _selectionRange.length);
}
@end
错误:i386硬件架构 未定义的符号: “_OBJC_IVAR _ $ _ UITextField._selectionRange”,从引用: - NSString + Utilities.o中的[UITextField(Editing)deleteBkw] ld:找不到架构i386的符号 collect2:ld返回1退出状态我们
你试过添加'#import'吗? –
我刚添加到我的NSString + Utilities.h中。但是,同样的错误仍然存在。 – docchang