【问题标题】:Issues with including Objective-C class in my xcode project在我的 xcode 项目中包含 Objective-C 类的问题
【发布时间】:2020-04-02 19:32:43
【问题描述】:

我对 Objective-C 比较陌生,大约有 1 年的经验,我在尝试向我的项目中添加一个类时遇到了问题。当我添加一个包含 XIB 文件的 UIViewController 子类时,我对此完全没有问题,xcode 以这种方式工作得很好。

但是,我尝试将一个简单的 Objective-C 类添加到名为 Test 的项目中,并使用以下 .h 和 .m 文件,并且遇到了代码编译和构建没有错误但方法 TestMethod 始终返回 nil 的问题.这里可能有什么问题?

测试.h

#import <Foundation/Foundation.h>

@class Test;

@interface Test : NSObject {

}

- (NSString *)TestMethod;

@end

测试.m

#import "Test.h"

@implementation Test

- (NSString*)TestMethod {
    return @"Test";
}

@end

在我的带有 XIB 文件的 UIViewController 子类中,该子类可以正常工作,但是当我尝试在其中包含我的 Test 类时,TestMethod 方法什么也不返回,即使它被硬编码为始终返回相同的字符串:

#import "Test.h"

Test *testobject;

// this compiles and builds but returns nothing
NSString *testString = [testobject TestMethod];

【问题讨论】:

    标签: iphone ios objective-c xcode subclass


    【解决方案1】:

    你错过了 alloc + init。

    使用

    Test *testobject=[[Test alloc] init];
    

    Test *testobject=[Test new];
    

    当您的对象未初始化时,您将获得nil 值。

    编辑:

    ARC 中:默认初始化。

    MRC 中:该值可能未初始化(垃圾值)。

    【讨论】:

    • 没有一个答案可以区分:当您的对象未初始化时,您将获得 nil 值。 -- 它在 ARC 中默认初始化。在 MRC 中,该值可能未初始化(垃圾值)。
    • @justin:那么答案中应该还有什么?
    • @justin - 在 MRC 中,只有在创建对象然后释放时才会发生这种情况。作者的帖子不是这样的。
    • @justin:当然……我会接受你提到的改变。
    • @justin 你是对的,因为它是一个本地的、非静态的变量。 (我记得 MRC 中实例变量的行为。)
    【解决方案2】:

    TestMethod 没有返回 nil - testobject 为零。

    改变

    Test *testobject;

    Test *testobject = [[Test alloc] init];

    【讨论】:

      【解决方案3】:

      你还没有创建一个 Test 实例,所以 testObject 只是持有 nil。您需要为变量分配一个 Test 实例才能执行您想要的操作。

      【讨论】:

        【解决方案4】:

        你也可以采用这种方法

        //Test.h
        
        #import <Foundation/Foundation.h>
        
        @class Test;
        
        @interface Test : NSObject {
        }
        - (id)init;
        -(NSString*)TestMethod;
        
        @end
        

        现在在您的 Test.m 文件中

        //Test.m

        #import "Test.h"
        
        @implementation Test
        
        
        - (id)init {
        
             if (self=[super init]) {
        
             }
             return self;
        }
        
        -(NSString*)TestMethod {
        return @"Test";
        }
        
        @end
        

        现在如果你想在另一个类中调用这个测试类,你必须创建一个测试类的实例。

        Test *testobject = [[Test alloc] init];
        
        NSString *testString = [testobject TestMethod];
        

        【讨论】:

          【解决方案5】:

          要访问类的任何方法/属性,首先需要使用 alloc/new 方法为该类的对象分配内存。

          由于您创建了该类类型的变量&lt;Test *testobject&gt;。但是变量没有分配任何内存,默认情况下它将为零。使用“nil”,您可以调用目标 C 中的任何方法。它不会崩溃。但它会返回 nil。

          因此,在访问任何对象之前,您必须为该对象创建内存

          Test *testobject = [Test alloc];
          

          使用默认构造函数(init、initWith 等)初始化对象

          [testobject init];
          

          现在对象已准备好调用实例方法/setter/getter 等...

          NSString *testString = [testobject TestMethod];
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2011-12-26
            • 1970-01-01
            • 2022-01-05
            • 2011-01-08
            • 1970-01-01
            • 2011-04-26
            相关资源
            最近更新 更多