开发者

Accessing private variable from Category generates linker error

开发者 https://www.devze.com 2022-12-09 13:27 出处:网络
I have the following code: @interface UIAcceleration (Simulation) - (id) initWithTimestamp:(NSTimeInterval)aTimeStamp

I have the following code:

@interface UIAcceleration (Simulation)

- (id) initWithTimestamp:(NSTimeInterval)aTimeStamp
                     X:(UIAccelerationValue)ax
                     Y:(UIAccelerationValue)ay
                     Z:(UIAccelerationValue)az;

@end

@implementation UIAcceleration (Simulation)

-(id)initWithTimestamp:(NSTimeInterval)aTimeStamp
                     X:(UIAccelerationValue)ax
                     Y:(UIAccelerationValue)ay
                     Z:(UIAccelerationValue)az 
{
    if (self = [super init])
    {
        timestamp = aTimeStamp;
        x = ax;
        y = ay;
        z = az;
    }

    return self;
}       

@end

when compiling this for simulator SDK everything works fine. When compiling for device SDK I get the following linker error:

  "_OBJC_IVAR_$_UIAcceleration.x", referenced from:
      _OBJC_IVAR_$_UIAcceleration.x$non_lazy_ptr in UIAcceleration+IRowAdditions.o
  "_OBJC_IVAR_$_UIAcceleration.y", referenced from:
      _OBJC开发者_Go百科_IVAR_$_UIAcceleration.y$non_lazy_ptr in UIAcceleration+IRowAdditions.o
  "_OBJC_IVAR_$_UIAcceleration.z", referenced from:
      _OBJC_IVAR_$_UIAcceleration.z$non_lazy_ptr in UIAcceleration+IRowAdditions.o
  "_OBJC_IVAR_$_UIAcceleration.timestamp", referenced from:
      _OBJC_IVAR_$_UIAcceleration.timestamp$non_lazy_ptr in AccelerometerSimulation.o
ld: symbol(s) not found
collect2: ld returned 1 exit status

What am I doing wrong? What is different between linking against Simulator frameworks (namely UIKit) and linking against device frameworks?


I think you probably included only the simulator version of the UIKit framework. I'm not so sure about this, but go to the left pane in Xcode and under the Frameworks folder, right click on UIKit.framework and Get Info. On mine, the path is /System/Library/Frameworks/UIKit.framework and right under that it says Path Type: "Relative to Current SDK". That should automatically pull the correct framework depending on what you're building for.

Worth a check.


This may be related to this issue with categories on the iPhone. Try adding -ObjC (iPhone OS 2.x) and -all_load (iPhone OS 3.0) to your "Other Linker Flags".


Ok, as none of the answers given helped, I did something diferent.

I created a class with exactly the same interface as UIAcceleration. Because both of them have the same memory layout, I can safely cast between pointers of UIAcceleration and my class. At least as long as gcc wants.

0

精彩评论

暂无评论...
验证码 换一张
取 消