7

MyClass.h

@interface MyClass : NSObject
@end

MyClass.m

// Define a private variable in a class extension
@interface MyClass () {
    NSString *name;
}
@end

然后在 lldb 中:

(lldb) po myClassInstance->name
error: 'MyClass' does not have a member named 'name'
error: 1 errors parsing expression

那么如何在调试器中访问该变量呢?

使用 xcode 4.3.2

谢谢!

4

3 回答 3

6

(lldb) po [myClassInstance valueForKey:@"name"]

于 2012-04-26T03:03:28.310 回答
0

直接干净地访问这些实例变量的唯一方法是通过提供有用功能的 Objective-C 运行时object_getInstanceVariable。该值通过引用传回,并且可以是许多不同的类型,因此在调试器中它不是很有用。但是你的问题启发了我想出一个解决方案。

我在 NSObject 上编写了一个类别,允许您从调试器内省实例变量,而不必担心访问器的副作用。将类别添加到项目后,您可以执行以下操作:

(lldb) po [self valueOfInstanceVariable:@"_name"]
IMG_4078.PNG

这是代码:

NSObject+IvarIntrospection.h

#if DEBUG
#import <Foundation/Foundation.h>

@interface NSObject (IvarIntrospection)

- (id)valueOfInstanceVariable:(NSString *)ivarName;

@end

#endif

NSObject+IvarIntrospection.m

#if DEBUG
#import "NSObject+IvarIntrospection.h"
#import <objc/runtime.h>

@implementation NSObject (IvarIntrospection)

- (id)valueOfInstanceVariable:(NSString *)ivarName {
    // Get the value of the instance variable
    // Use a union in order to convert the value to a float or double (see http://en.wikipedia.org/wiki/Type_punning)
    union {
        void *value;
        float f;
        double d;
    } ivar;
    Ivar ivarInfo = object_getInstanceVariable(self, [ivarName UTF8String], &ivar.value);

    // If the instance variable doesn't exist, try adding an underscore
    if (!ivarInfo && ![ivarName hasPrefix:@"_"]) {
        NSString *underscoredIvarName = [@"_" stringByAppendingString:ivarName];
        NSLog(@"Instance variable '%@' does not exist. Perhaps you meant '%@?' Let's try that.", ivarName, underscoredIvarName);

        return [self valueOfInstanceVariable:underscoredIvarName];

    // If there's already an underscore, error
    } else if (!ivarInfo) {
        NSLog(@"Instance variable '%@' does not exist.", ivarName);
        return nil;
    }

    // Figure out what type the instance variable is and return a sensible representation
    const char *type = ivar_getTypeEncoding(ivarInfo);
    switch (type[0]) {
        case 'c':
            return [NSNumber numberWithChar:(char)ivar.value];
        case 'i':
            return [NSNumber numberWithInt:(int)ivar.value];
        case 's':
            return [NSNumber numberWithShort:(short)ivar.value];
        case 'l':
            return [NSNumber numberWithLong:(long)ivar.value];
        case 'q':
            return [NSNumber numberWithLongLong:(long long)ivar.value];
        case 'C':
            return [NSNumber numberWithUnsignedChar:(unsigned char)ivar.value];
        case 'I':
            return [NSNumber numberWithUnsignedInt:(unsigned int)ivar.value];
        case 'S':
            return [NSNumber numberWithUnsignedShort:(unsigned short)ivar.value];
        case 'L':
            return [NSNumber numberWithUnsignedLong:(unsigned long)ivar.value];
        case 'Q':
            return [NSNumber numberWithUnsignedLongLong:(unsigned long long)ivar.value];
        case 'f':
            return [NSNumber numberWithFloat:ivar.f];
        case 'd':
            return [NSNumber numberWithDouble:ivar.d];
        case '*':
            return [NSString stringWithUTF8String:(const char *)ivar.value];
        case '@':
        case '#':
            return (id)ivar.value;
        case ':':
            return NSStringFromSelector((SEL)ivar.value);
        default:
            return [NSValue valueWithBytes:&ivar.value objCType:type];
    }
}

@end

#endif

请注意,编译发布时该类别将被自动禁用(感谢调试宏)。

于 2014-05-09T06:05:10.400 回答
-3

如果您需要nameMyClass方法外部访问,则需要定义方法来访问它。您可以只编写名为(NSString*) nameand的方法- (void) setName:(NSString*) newName,但定义属性和合成它们更容易。

MyClass.h中,您定义了一个属性。对于字符串,您通常制作它们copy

@interface MyClass : NSObject

@property (copy) NSString* name;

@end

MyClass.m中,您仍然使用带有 ivar 的接口声明:

@interface MyClass () {
    NSString *name;
}
@end

但是,您还需要合成新属性。这将创建用于检索和设置name的方法:

@implementation MyClass

@synthesize name = name;

@end

作为约定,通常在 ivar 的开头或结尾放置下划线,因此在接口中您将拥有NSString *_name;,而在实现中您将拥有@synthesize name = _name。这有助于避免在您指的是属性时意外使用 ivar。

您现在可以访问您的name属性:

MyClass me = [[[MyClass alloc] init] autorelease];
[me setName:@"My name"];
NSLog(@"Name = %@", [me name]);

Objective-C 属性是该语言的一个强大功能,但它们确实有一些你必须学习的怪癖。尝试在网络上搜索“objective-C”、“properties”和“synthesize”的组合。

如果您仍然有编译器错误,请使用您访问的代码部分编辑您的问题name

于 2012-04-26T02:57:09.003 回答