【发布时间】:2011-11-02 17:41:49
【问题描述】:
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”,引用自: -[UITextField(Editing) deleteBkw] 在 NSString+Utilities.o ld:未找到体系结构 i386 的符号 collect2: ld 返回 1 个退出状态
【问题讨论】:
-
你试过加
#import <UIKit/UIKit.h>吗? -
我刚刚添加到我的 NSString+Utilities.h 中。但是,同样的错误仍然存在。
标签: objective-c ios categories instance-variables private-members