How can I find out the Objective-C generics type?
The lightweight generics introduced in Xcode 7 are just compile time hints to help the compiler raise warnings, but at run time you get the same old behavior with your variable being just NSArray
s of id
s.
Source: WWDC '15 "Swift and Objective-C Interoperability" session
See the transcript of the talk:
So the entire lightweight generics feature is based on a type erasure model. Which means that the compiler has all of this rich static type information but it erases that information when generating code.
Generic Type In Objective C
Thats what id
is in Objective-C, a untyped Objective-C object pointer.
id test = [[NSMutableArray alloc] init];
etc
How can I create a generic property in Objective-C?
Here is answer.
@interface myParentView< T: parentModel*> :UIView
@property T myObject; // myObject is object of parentModel
@end
In all subclass:
@interface myChildViewOne :myParentView<childModel>
// Now myObject is object of childModel
@end
Obj C has complicated syntax, But we can achieve generic property like above.
lightweight generics in objective c
Objective-C generics really are lightweight, and were added to improve interoperability with Swift, not to make Objective-C code safer. Similar to nullability, think of generics as a way to annotate your interface rather than a reason to change your implementation.
Changing ObjectType
to id
in the implementation is the best way forward.
Further reading: article on Objective C Generics. Read bob’s comment on that article if you want to know about __covariant
.
Using a generic Swift class in Objective-C
Swift generic types cannot be used in Objective-C.
https://developer.apple.com/library/content/documentation/Swift/Conceptual/BuildingCocoaApps/MixandMatch.html#//apple_ref/doc/uid/TP40014216-CH10-ID136
This excludes Swift-only features such as those listed here:
- Generics
- ...
In objective-c generics can i pass the type from 1 class to another
Objective-C does not have generics, Apple introduced something they term Lightweight Generics, and you may read the "lightweight" as "faux".
Here is Apple's own statement on what they do:
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.
Notice the "improves Swift access" and the lack of "improves Objective-C". These lightweight generics are essentially annotations for the Swift compiler when it is importing Objective-C types. On the Objective-C side maintaining the generic invariants is largely the responsibility of the programmer.
You can use the lightweight generic annotations (a) in an @interface
and (b) when declaring a variable whose type has lightweight generic annotations. You cannot otherwise use the annotations, in particular you do not use them in an @implementation
other than (b) above. Within the @implementation
for a lightweight generic annotated @interface
rather than use the "type parameters" from the annotations you use the id
type - as you have done with your doSomething:
method.
The latest Xcode/Clang will provide some checking of lightweight generic annotations in Objective-C, but it is limited and should in no way be taken as a guarantee that the generic conditions are being checked.
In summary, unless you are planning to interwork with Swift using the lightweight generic annotations in Objective-C is of little benefit, and it certainly does not give you "generics" in the usual sense (i.e. parametric generic types).
Undoubtedly not what you wanted to hear, but HTH
Related Topics
How to Print Out a Property's Contents Using Xcode Debugger
Cllocationmanager Authorization Issue iOS 8
iOS Facebook Login "Given Url Is Not Allowed by the Application Configuration"
Avplayer with Playback Controls of Avplayerviewcontroller
Confirm Back Button on Uinavigationcontroller
How to Remove the Bottom Gap of Uipageviewcontroller
Check If 3D Touch Is Supported and Enabled on the iOS9 Device
Mapkit Annotations Disappearing
iOS 7.0 and Arc: Uitableview Never Deallocated After Rows Animation
Code Signing Issue in Xcode Version 8
iOS Lazy-Loading of Table Images
iOS 11 - Unable to Change Navigation Bar Height
How to Take Uiimage of Avcapturevideopreviewlayer Instead of Avcapturephotooutput Capture
Custom Uisearchbar with Uisearchcontroller
Making Uitableview with Embedded Uicollectionview Using Uitableviewautomaticdimension