objective-c - 无法从 C 函数调用 Objective C 方法

标签 objective-c

我正在为 GUI 应用程序使用 MVC 架构。模型类有一些 C 函数。其中一个 C 函数调用了 Objective-C 类的一些方法。我使用该类的对象调用这些方法。奇怪的是,xyz 方法之前的方法被完美地调用,但是当调用该 xyz 方法时,该方法和它之后的方法都没有被执行。 我没有收到任何错误。所以无法弄清楚到底发生了什么。 关于这可能是什么原因的任何想法?

最佳答案

正如 Marc 指出的那样,您可能正在使用对 OBJC 对象的引用,该对象在 objective-c 范围之外未初始化。

这是调用 ObjC 对象方法的 C 代码的工作示例:

#import <Cocoa/Cocoa.h>

id refToSelf;

@interface SomeClass: NSObject
@end

@implementation SomeClass
- (void) doNothing
{
        NSLog(@"Doing nothing");
}
@end

int otherCfunction()
{
        [refToSelf doNothing];
}


int main()
{

    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

    SomeClass * t = [[SomeClass alloc] init];
    refToSelf = t;

    otherCfunction();

    [pool release];
}

关于objective-c - 无法从 C 函数调用 Objective C 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/304885/

相关文章:

objective-c - 运行shell脚本objective-c的最简单方法

ios - 从 didFinishLaunchingWithOptions 终止应用程序

ios - 如何确定应用程序是否已启动以进行后台获取

IOS TableView :Table View Custom Cell While Scrolling It pre-populates the Value in another cell

ios - 为什么从 UITextField 输入的字符串中四舍五入到较低的整数?

objective-c - 为什么我的iPhone不振动?

objective-c - UIAlertView 仅在关闭后才显示

objective-c - popToRootViewControllerAnimated 不显示 Root View Controller

objective-c - 将 Swift 值类型与对象(即运行时的引用类型)区分开来?

iphone - Objective-C,引用属性