Template in Objective C?

2019-06-14 23:27发布

Everything is in the title :)

Is there any templates in ObjC ?

I need equivalent of c# :

public class MyClass<T> : Message

in ObjC .

Any helps will be strongly thanks :(

3条回答
疯言疯语
2楼-- · 2019-06-15 00:06

By the way, Xcode supports adding C++ classes through the New->File. Using the extern "C" {} construct in C++ means you can provide as much or as little C-callable interface as you need, which you can then call directly from your Objective-C code, since Objective-C is a superset of C.

Having said that, it's probably a good idea to stick within the Objective-C paradigm unless you have a pressing reason to move outside it, such as the need to incorporate a body of existing C++ code into your project. (That's not to say that Objective-C is a "better" language, which is a different matter entirely.)

查看更多
倾城 Initia
3楼-- · 2019-06-15 00:18

There is no such ObjC feature. While ObjC++ does exist, I strongly discourage its broad use. It has many problems from poor tool and debugger support, to poor compiler optimization, to degraded ARC performance.

Generally templates are not required in ObjC because it is not a strongly typed language. An NSArray can hold any object, so you don't need to use a template to get the right type. Do you have a specific problem you're trying to solve? There is likely a better ObjC solution.

查看更多
Luminary・发光体
4楼-- · 2019-06-15 00:30

Obj-C supports templates since Xcode v7. It is named generics:

Lightweight generics now allow you to specify type information for collection classes such as NSArray, NSSet, and NSDictionary. The type information improves Swift access when you bridge from Objective-C, and simplifies the code you have to write. For example:

NSArray<UIImage *> *images; 
NSDictionary<NSString *, NSURL *> *resourcesByName;

Look for "Objective-C Language Changes" section in https://developer.apple.com/library/content/documentation/Xcode/Conceptual/RN-Xcode-Archive/Chapters/xc7_release_notes.html

查看更多
登录 后发表回答