我可以在 Objective-C 中重载运算符吗?

发布于 2024-09-17 20:06:11 字数 127 浏览 5 评论 0原文

是否可以覆盖 Objective-C 中的运算符使用?

例如

myClassInstance + myClassInstance

调用自定义函数将两者相加。

Is it possible to override operator use in Objective-C?

For example

myClassInstance + myClassInstance

calls a custom function to add the two.

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

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

发布评论

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

评论(7

痞味浪人 2024-09-24 20:06:11

运算符重载不是 Objective-C 的特性。如果您的类的两个实例可以添加在一起,请提供一个方法并允许使用该方法添加它们:

Thing *result = [thingOne thingByAddingThing:thingTwo];

或者,如果您的类是可变的:

[thingOne addThing:thingTwo];

Operator overloading is not a feature of Objective-C. If two instances of your classes can be added together, provide a method and allow them to be added using that method:

Thing *result = [thingOne thingByAddingThing:thingTwo];

Or, if your class is mutable:

[thingOne addThing:thingTwo];
吐个泡泡 2024-09-24 20:06:11

不,你不能在 Objective-C 中做到这一点。

No, you can't do this in Objective-C.

寻梦旅人 2024-09-24 20:06:11

您现在可以在 objC 的后继者 Swift 中执行此操作。由于 Objective-C 和 Swift 是一起工作的 您可能会感兴趣。

You can do this now in Swift, a successor to objC. And since Objective-C and Swift are made to work together This could be interesting for you.

世界和平 2024-09-24 20:06:11

您可能希望支持对象的订阅。下标不是运算符重载,但它对于集合对象来说很方便。 NSArray 和 NSDictionary 都支持下标。例如:

NSMutableArray *a = [NSMutableArray new];
a[0] = @"Hello";

支持索引下标的方法是实现以下内容:

-(id)objectAtIndexedSubscript:(NSUInteger)idx;
-(void)setObject:(id)newObject atIndexedSubscript:(NSUInteger)idx];

You may want to support subscripting for your object. Subscripting is not operator overloading, but it can be handy for a collection object. NSArray and NSDictionary both support subscripting. For example:

NSMutableArray *a = [NSMutableArray new];
a[0] = @"Hello";

The way to support index subscripting is to implement the following:

-(id)objectAtIndexedSubscript:(NSUInteger)idx;
-(void)setObject:(id)newObject atIndexedSubscript:(NSUInteger)idx];

请叫√我孤独 2024-09-24 20:06:11

我知道这是一个老问题,但我只是想把这个答案留在这里,供将来可能想知道这是否有可能的人使用。

答案是肯定的!

您必须使用 Objective-C 的一个变体,称为 Objective-C++。
举个例子,假设您创建了一个新的 Objective-C 命令行工具项目。为了允许 C++ 功能,您需要将“main.m”重命名为“main.mm”。然后,您可以将 C++ 代码与 Objective-C 代码混合在同一个文件中。虽然存在一些限制,但我已经测试了运算符重载,据我所知,它似乎与 Objective-C 对象完美配合。
我提供了示例源代码,让您了解如何执行此操作:

//main.mm
#import <Foundation/Foundation.h>
#include <iostream>
#include <string>

std::ostream &operator<<(std::ostream &os, NSString *s) {
    os << [s UTF8String];
    return os;
}

int main(int argc, const char * argv[]) {
    @autoreleasepool {

        NSString *str = @"I'm an NSString!";
        std::cout << str << std::endl;

    }
    return 0;
}

这是构建并运行此代码后的输出:

我是 NSString!
程序以退出代码结束:0

希望这对某人有帮助!

I know this is an old question but I just wanted to leave this answer here for anybody in the future that might want to know if this is a possibility.

The answer is YES!

You'll have to use a variant of Objective-C called Objective-C++.
As an example, say you created a new Objective-C command-line tool project. In order to allow C++ functionality, you'll need to rename "main.m" to "main.mm". Afterwards, you can mix C++ code in with your Objective-C code in the same file. There are some limitations, but I've tested operator overloading and it seems to work perfectly fine with Objective-C objects as far as I can tell.
I've included sample source code to give you an idea of how to do it:

//main.mm
#import <Foundation/Foundation.h>
#include <iostream>
#include <string>

std::ostream &operator<<(std::ostream &os, NSString *s) {
    os << [s UTF8String];
    return os;
}

int main(int argc, const char * argv[]) {
    @autoreleasepool {

        NSString *str = @"I'm an NSString!";
        std::cout << str << std::endl;

    }
    return 0;
}

Here's my output after building and running this code:

I'm an NSString!
Program ended with exit code: 0

Hopefully this will be of help to somebody!

此刻的回忆 2024-09-24 20:06:11

不,Objective-C 不支持运算符重载。

No, Objective-C does not support operator overloading.

奈何桥上唱咆哮 2024-09-24 20:06:11

首先,运算符重载是邪恶的。其次,C 没有运算符重载,并且 Objective-C 是 C 的适当超集,它仅添加了少量关键字和消息传递语法。

话虽这么说,如果您使用 Apple 的开发环境,则可以使用 Objective-C++ 而不是 Objective-C,这使您可以访问 C++ 的所有错误和缺陷,包括运算符重载。使用 Objective-C++ 最简单的方法就是将实现文件的扩展名从“.m”更改为“.mm”

First, operator overloading is evil. Second, C doesn't have operator overloading, and Objective-C is a proper superset of C, which only adds a handful of keywords and a messaging syntax.

That being said, if you're using Apple's development environment, you can use Objective-C++ instead of Objective-C, which gives you access to all of C++'s mistakes and misfeatures, including operator overloading. The simplest way to use Objective-C++ is just to change the extension on your implementation files from ".m" to ".mm"

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