Objective-C 导入,使用 Cygwin 的原始类型

发布于 2024-07-26 07:54:11 字数 95 浏览 7 评论 0原文

我了解 Objective-C 的基本语法,已经安装了 Cygwin,并且想要进行实验。 但是我不确定两件事: 我要导入什么,以及 原始类型名称是什么。 有人能帮我吗?

I understand the basic syntax of Objective-C, have installed Cygwin, and want to experiment. However i am unsure of two things:
What i would import, and
what the primitive type names are.
Can someone help me?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

怪我入戏太深 2024-08-02 07:54:11

您可以继承的唯一对象称为Object。 请记住,它提供的功能远不及 NeXTStep 或 Cocoa 的 NSObject 的功能。 Object 甚至没有像引用计数这样的东西。 为了获得与 NSObject 相同类型的引用计数内存管理,您需要自己实现它。

#import <objc/Object.h>

@interface MyObject : Object
{
    int retainCount;
}

- (id) retain;
- (int) retainCount;
- (void) release;
@end

@implementation MyObject

+ (MyObject *) alloc
{
    // In Cocoa, allocated objects have an implicit retain.
    MyObject *anObject = [super alloc];
    anObject->retainCount = 1;
    return anObject;
}

- (void) release
{
    retainCount--;
    if (retainCount == 0)
        [self free];
}

- (id) retain
{
    retainCount++;
    return self;
}

- (int) retainCount
{
    return retainCount;
}
@end


int main (int argc, char *argv[])
{
    MyObject *test = [[MyObject alloc] init];
    [test retain];
    [test release];
    [test release];
    // (test should be deallocated now)

    return 0;
}

链接时,您必须确保与 -lobjc 链接,这是 Object 的定义所在(我认为)。

另一个大问题是静态字符串实例,即代码中出现 @"like this" 的字符串。 在 GNU 运行时中,字符串的静态实例需要具有特定的 ivar 布局,即:

// Let's assume that we have a protocol <MyObject> that defines all the basic methods
// like retain, release etc. In this case, these should be no-ops because the static
// string is never deallocated. In Cocoa, there is a protocol <NSObject> which provides
// the same common methods.

@interface MyStaticStringClass : Object <MyObject>
{
    char *str;
    unsigned len;
}
- (const char *) cString;
@end

@implementation MyStaticStringClass
- (void) retain
{
    return;
}

- (id) retain
{
    return self;
}

- (int) retainCount
{
    return INT_MAX;
}

- (const char *) cString
{
    return str;
}
@end

int main (int argc, char *argv[])
{
    id aString = @"Hello world!";

    fprintf (stdout, "aString has the contents: %s\n", [aString cString]);

    return 0;
}

编译时,可以使用标志 -fconstant-string-class=MyStaticStringClass。 您可以为字符串类提供任何您喜欢的方法,但它必须只有两个 ivars,并且它们的顺序必须正确。 如果你不想使用 Objective-C 风格的字符串,那么你不必定义静态字符串类。 如果您确实定义了静态字符串类,它应该能够复制动态字符串类(即在运行时分配的字符串对象)的行为,以便您可以在给定情况下使用其中任何一个。

对于命令行实用程序和基本应用程序,我选择不使用 Cocoa 或 GNUstep,而是定义我自己的类。 它有很多缺点,但我发现 Objective-C 中的对象抽象和变形比我编程的其他语言更容易实现。

The only object you can inherit from is called Object. Bare in mind that this offers nowhere near the same amount of functionality as NeXTStep's or Cocoa's NSObject. Object does not even have anything like reference counting. In order to get the same sort of reference counting memory management that NSObject has you'll need to implement it yourself.

#import <objc/Object.h>

@interface MyObject : Object
{
    int retainCount;
}

- (id) retain;
- (int) retainCount;
- (void) release;
@end

@implementation MyObject

+ (MyObject *) alloc
{
    // In Cocoa, allocated objects have an implicit retain.
    MyObject *anObject = [super alloc];
    anObject->retainCount = 1;
    return anObject;
}

- (void) release
{
    retainCount--;
    if (retainCount == 0)
        [self free];
}

- (id) retain
{
    retainCount++;
    return self;
}

- (int) retainCount
{
    return retainCount;
}
@end


int main (int argc, char *argv[])
{
    MyObject *test = [[MyObject alloc] init];
    [test retain];
    [test release];
    [test release];
    // (test should be deallocated now)

    return 0;
}

When linking, you have to make sure you link with -lobjc, this is where the definition of Object lies (I think).

The other big catch is with static string instances, i.e. strings in code that appear @"like this". With the GNU runtime, static instances of strings need to have a particular ivar layout, which is:

// Let's assume that we have a protocol <MyObject> that defines all the basic methods
// like retain, release etc. In this case, these should be no-ops because the static
// string is never deallocated. In Cocoa, there is a protocol <NSObject> which provides
// the same common methods.

@interface MyStaticStringClass : Object <MyObject>
{
    char *str;
    unsigned len;
}
- (const char *) cString;
@end

@implementation MyStaticStringClass
- (void) retain
{
    return;
}

- (id) retain
{
    return self;
}

- (int) retainCount
{
    return INT_MAX;
}

- (const char *) cString
{
    return str;
}
@end

int main (int argc, char *argv[])
{
    id aString = @"Hello world!";

    fprintf (stdout, "aString has the contents: %s\n", [aString cString]);

    return 0;
}

When compiling, you can use the flag -fconstant-string-class=MyStaticStringClass. You can provide whatever methods you like for the string class but it must have only two ivars and they must be in the right order. If you don't want to use Objective-C style strings, then you don't have to define a static string class. If you do define a static string class it should be able to replicate the behaviour of your dynamic string class (i.e. string objects that are allocated during run time) so that you can use either in a given situation.

For command-line utilities and basic apps I choose not to use Cocoa or GNUstep but rather define my own classes. It has many drawbacks, but I find that object abstraction and metamorphism in Objective-C is much easier to implement than in the other languages that I program in.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文