swift-extensions

Protocol methods in a class extension are not called under specific conditions

六月ゝ 毕业季﹏ 提交于 2019-12-11 06:24:27
问题 I encountered a weird behavior. The best way I can put it is … Not overridden protocol methods in a class extension are not called while the superclass already conforms to the protocol (via extension) . However this happens only while it's build with the release build configuration. class A: UIViewController {} extension A: UIScrollViewDelegate { func scrollViewDidScroll(_ scrollView: UIScrollView) { print("scrollViewDidScroll in superclass") } } class B: A { // A tableView (and its data

Swift 3: Add UIButton extension to ViewController

巧了我就是萌 提交于 2019-12-11 06:06:17
问题 I'm a beginner in iOS/Swift and trying to create a simple application without Storyboard. I created a UIButton extension and I would like to add a simple button to my view (constraints will be set later). Unfortunately the button is not visible. I would appreciate if somebody helps me. Thank you! --- Buttons.swift --- extension UIButton { func createRectangleButton(buttonPositionX: Double, buttonPositionY: Double ,buttonWidth: Double, buttonHeight: Double, buttonTilte: String) { let button =

.sort in protocol extension is not working

泄露秘密 提交于 2019-12-11 04:45:36
问题 I have a protocol as well as a protocol extension and I'd like to implement a function in the protocol extension to sort an array defined by the protocol with custom objects, but it's not working. protocol MyProtocol { var myArray: [MyObject] { get set } } extension MyProtocol { func sortArrayByCreationTime() { myArray.sort { $0.created > $1.created } } } Xcode is telling me that 'sort' has been renamed to 'sorted(by:)', but if im using this a new array gets created, but I need the old array

Cannot override initializer of NSDictionary in Swift

这一生的挚爱 提交于 2019-12-10 14:41:00
问题 I'm trying to extend the class NSDictionary in Swift to contain an NSDate that is set on init(). When I add my custom init(), I get the complier error: 'required' initializer 'init(dictionaryLiteral:)' must be provided by subclass of 'NSDictionary' However, when I add that initializer using auto-complete, I get the following error: Declarations from extensions cannot be overridden yet Is there any way to override the initializer of NSDictionary or can Swift just not handle that yet? Here's my

Extending Dictionary with key and value constraints

☆樱花仙子☆ 提交于 2019-12-10 11:02:27
问题 I want to extend dictionary and constrain it to specific key type of NSDate with values of type Array<MyClass> MyClass is a swift class with no subclass. extension Dictionary where Key: NSDate, Value: Array<MyClass>{ func myFunction() -> Int{ // Some thing useful } } The above code returns a build error: Type 'Value' constrained to non-protocol type 'Array' Ok so it seems that it needs to be constrained to a specific protocol, but strangely enough when I constrain Value to: extension

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

老子叫甜甜 提交于 2019-12-10 10:27:35
问题 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

Swift convenience initializer extension for SKPhysicsBody

泄露秘密 提交于 2019-12-10 10:13:30
问题 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 ***

Self.Type cannot be directly converted to AnyClass in extension to objective-c class in swift

不想你离开。 提交于 2019-12-08 16:37:33
问题 I'm trying to create fabric method to create UIViewController with correct nib name (to fix iOS8 default initialiser issue). To do it I have added extension: extension UIViewController { class func create() -> Self { if #available(iOS 9.0, *) { return self.init() } else { let clsName = NSStringFromClass(self).componentsSeparatedByString(".").last! return self.init(nibName: clsName, bundle: nil) } } } However compiler issues error: Cannot convert value of type 'Self.Type' to expected argument

Can't assign the item in _ArrayProtocol

痞子三分冷 提交于 2019-12-08 11:58:26
问题 Hi I'm trying to retrieve data by using ParseLiveQuery My problem is...I can't assign the item in Array.. import Foundation import Parse import ParseLiveQuery extension _ArrayProtocol where Iterator.Element == PFObject { mutating func updateWithEvent(event: Event<PFObject>) { switch event { case .created(let object): append(object) case .entered(let object): append(object) case .deleted(let object): if let index = index(of: object) { remove(at: index) } case .left(let object): if let index =

Handling class initialization failure in Swift extension

大城市里の小女人 提交于 2019-12-06 19:27:23
问题 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