我是objc/Cocoa的新手,我從來沒有使用過C語言。在Obj-c中訪問先前定義的C Struct數據的問題
我已經得到先前定義的C結構數據的問題...
這裏是我的代碼:
AppController.h
#import <Cocoa/Cocoa.h>
@interface AppController : NSObject {
AuthorizationRef authRef;
AuthorizationRights authRights;
AuthorizationFlags authFlags;
}
- (IBAction)toggleAuthentification:(id)sender;
@end
AppController.m
#import "AppController.h"
@implementation AppController
- (id)init {
if (![super init])
return nil;
AuthorizationCreate(NULL, kAuthorizationEmptyEnvironment, kAuthorizationFlagDefaults, &authRef);
AuthorizationItem rightItems[1] = {{"com.myname.myapp.adminRights", 0, NULL, 0}};
authRights.count = 1;
authRights.items = rightItems;
authFlags = kAuthorizationFlagDefaults | kAuthorizationFlagExtendRights | kAuthorizationFlagInteractionAllowed | kAuthorizationFlagPreAuthorize;
return self;
}
- (IBAction)toggleAuthentification:(id)sender {
NSLog(@"%d", AuthorizationCopyRights(authRef, &authRights, kAuthorizationEmptyEnvironment, authFlags^kAuthorizationFlagInteractionAllowed, NULL));
}
@end
當我點擊在我的應用程序的一個按鈕調用toggleAuthentification:
,我得到錯誤代碼-60008(errAuthorizationInternal)。
在調試器中,我可以看到authRights.count
= 1,表示正確,但authRights.items
與init
中定義的數據沒有任何對應關係。
我嘗試了很多不同的方式,但是我找不到解決方案。
請,任何人都可以解釋爲什麼它不工作,就像我會工作,以及如何解決我的問題。
膽紅素
感謝Bavarious的解釋和解決方案,它的工作完美。我在想,函數範圍中定義的變量和全局變量中引用的變量會自動保留在內存中。現在我將使用'self = [super init];如果(!self)返回零;' – Bil 2011-04-13 11:18:00