You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Becomes simply item.index = index. With Swift really forcing type-safe environment, where are no implicit conversions even between Int and UInt, this is a place I need to go back to and add the casting manually.
Proposal:
if you are casting to a primitive value (integers, floats, boolean), do the following: ObjC: [item setIndex:(int)index]; --> Swift: item.index = Int32(index) (int is a 32-bit integer).
if you are casting to an enum, do the following: ObjC: value = (MyEnum)index; --> Swift: value = MyEnum(rawValue: index) - since casting directly to an enum isn't possible in Swift.
if you are casting to a class type (or id) use as?: ObjC: myVar = (NSTableView *)[self superview]; --> Swift: myVar = self.superview as? NSTableView. In case of id, use AnyObject. Standard class conversions apply - NSString -> String, NSArray -> [AnyObject], NSDictionary -> [NSObject : AnyObject].
if you are casting to anything else:
if it's a pointer (e.g. void *, or float *, or mystruct *), use the following: ObjC: floatArray = (float *)malloc(16) --> floatArray = UnsafePointer<Float>(malloc(16)) - (UnsafePointer<Void>, UnsafePointer<mystruct>, ...)
at this point, I can't think of another valid cast, in which case I'd throw an error.
The text was updated successfully, but these errors were encountered:
Currently, casting information is discarded:
Becomes simply
item.index = index
. With Swift really forcing type-safe environment, where are no implicit conversions even betweenInt
andUInt
, this is a place I need to go back to and add the casting manually.Proposal:
ObjC: [item setIndex:(int)index];
-->Swift: item.index = Int32(index)
(int
is a 32-bit integer).ObjC: value = (MyEnum)index;
-->Swift: value = MyEnum(rawValue: index)
- since casting directly to an enum isn't possible in Swift.id
) useas?
:ObjC: myVar = (NSTableView *)[self superview];
-->Swift: myVar = self.superview as? NSTableView
. In case ofid
, useAnyObject
. Standard class conversions apply -NSString
->String
,NSArray
->[AnyObject]
,NSDictionary
->[NSObject : AnyObject]
.void *
, orfloat *
, ormystruct *
), use the following:ObjC: floatArray = (float *)malloc(16)
-->floatArray = UnsafePointer<Float>(malloc(16))
- (UnsafePointer<Void>
,UnsafePointer<mystruct>
, ...)The text was updated successfully, but these errors were encountered: