swift-extensions

Can I extend Tuples in Swift?

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-06 18:17:52
问题 I'd like to write an extension for tuples of (e.g.) two value in Swift. For instance, I'd like to write this swap method: let t = (1, "one") let s = t.swap such that s would be of type (String, Int) with value ("one", 1) . (I know I can very easily implement a swap(t) function instead, but that's not what I'm interested in.) Can I do this? I cannot seem to write the proper type name in the extension declaration. Additionally, and I suppose the answer is the same, can I make a 2-tuple adopt a

How to use @objc protocol with optional and extensions at the same time?

只愿长相守 提交于 2019-12-06 13:43:50
This code does not compile and might sound stupid as it is, but i'll explain why it's so important! @objc protocol p { optional func f1() func f2() } extension p { func f1() { } func f2() { } } class foo: p { } Compiler says Type c does not conform to protocol 'p' and that's maybe because you can not use @objc optional and extensions at the same time (and does not make sence in this scenario either). But consider the following example: I want to set a selector on a non-optional method defined in protocol in my extension (main reason i used @objc): func f1() { } -> func f1() { ... #selector

keyPathsForValuesAffecting with NSManagedObject

跟風遠走 提交于 2019-12-06 03:23:20
问题 Bonjour, I would like to translate an objective'c exercise from an Aaron's book to swift but I can't find the solution. The Objective'c code is : @dynamic firstName; @dynamic lastName; @dynamic department; + (NSSet *)keyPathsForValuesAffectingFullName { return [NSSet setWithObjects:@"firstName", @"lastName", nil]; } - (NSString *)fullName { NSString *first = [self firstName]; NSString *last = [self lastName]; if (!first) return last; if (!last) return first; return [NSString stringWithFormat:

How to use a protocol with optional class methods in an extension with generic in Swift?

怎甘沉沦 提交于 2019-12-06 00:23:33
I am trying using extension for an existing class with class method like: @objc public protocol MyProtocol { optional class func foo() -> Int } And I am using this protocol in an extension with generic like: extension MyClass { public func bar<T: MyProtocol>() { ... let x: Int = T.self.foo!() // if I do not use "self" or "!" here, I will have a compiler error ... } This should work but when I build it, Xcode says "Command /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swiftc failed with exit code 1". If I do not use "optional" in the protocol, I do not

Swift convenience initializer extension for SKPhysicsBody

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-05 19:43:59
extension SKPhysicsBody { /// anchorPoint version of init(rectangleOfSize:center:) convenience init(rectangleOfSize s: CGSize, withAnchorPoint anchorPoint: CGPoint) { var center = CGPoint() center.x = (s.width / 2) - ( s.width * anchorPoint.x) center.y = (s.height / 2 ) - ( s.height * anchorPoint.y) self.init(rectangleOfSize: s, center: center) } } I got this error on runtime -[PKPhysicsBody initWithRectangleOfSize:withAnchorPoint:]: unrecognized selector sent to instance 0x7f9b03c4fff0 *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[PKPhysicsBody

Handling class initialization failure in Swift extension

末鹿安然 提交于 2019-12-05 00:40:39
I'm rewriting an Objective C category below to Swift: @implementation UIImage (Extra) + (UIImage *)validImageNamed:(NSString *)name { UIImage *image = [self imageNamed:name]; NSAssert(image, @"Unable to find image named '%@'", name); return image; } This asks to be implemented as an convenience init, but how can I check if designated initializer self.init(named:) succeeds? extension UIImage { convenience init(validateAndLoad name: String!) { self.init(named: name) // need to assert here if self.init fails } When self.init(named:) call fails, extension's init stops executing. I've tried

Can I extend Tuples in Swift?

◇◆丶佛笑我妖孽 提交于 2019-12-04 23:35:05
I'd like to write an extension for tuples of (e.g.) two value in Swift. For instance, I'd like to write this swap method: let t = (1, "one") let s = t.swap such that s would be of type (String, Int) with value ("one", 1) . (I know I can very easily implement a swap(t) function instead, but that's not what I'm interested in.) Can I do this? I cannot seem to write the proper type name in the extension declaration. Additionally, and I suppose the answer is the same, can I make a 2-tuple adopt a given protocol? You cannot extend tuple types in Swift. According to Types , there are named types

Generic IBDesginables UIView extension

百般思念 提交于 2019-12-04 19:30:55
I would like to create generic extension for class to add some designables functionality to any UIView subclass and by this avoid adding functionality to all subclasses. So, would be nice to add extension for UIView which conforms to protocol SomeProtocol (It's empty because it is just a tag to mark classes which I want functionality to be added). Then just add that protocol in any UIView subclass where I want that functionality to be implemented like this: protocol SomeProtocol { //empty } class BCBorderedView : UIView, SomeProtocol { //empty } class BCBorderedSearch: UISearchBar,

Swift - How can I override an extension method in a concrete subclass

别来无恙 提交于 2019-12-04 09:08:01
问题 I have an extension on UIView implementing a protocol protocol SomeProtocol { var property : Int } extension UIView : SomeProtocol { var property : Int { get { return 0 } set { // do nothing } } } in a concrete subclass I want to override this extension method: class Subclass : UIView, SomeProtocol { var _property : Int = 1 var property : Int { get { return _property} set(val) {_property = val} } } I set breakpoints and see that the extension method is called and not the concrete subclass

keyPathsForValuesAffecting with NSManagedObject

旧城冷巷雨未停 提交于 2019-12-04 07:24:24
Bonjour, I would like to translate an objective'c exercise from an Aaron's book to swift but I can't find the solution. The Objective'c code is : @dynamic firstName; @dynamic lastName; @dynamic department; + (NSSet *)keyPathsForValuesAffectingFullName { return [NSSet setWithObjects:@"firstName", @"lastName", nil]; } - (NSString *)fullName { NSString *first = [self firstName]; NSString *last = [self lastName]; if (!first) return last; if (!last) return first; return [NSString stringWithFormat:@"%@ %@", first, last]; } I found a function in the developer documentation but I can't understand how