标签: swift-extensions

协议实现协议的默认实现

我正在尝试创建一个由某些类实现的协议,所有这些都应该实现UIScrollViewDelegate.我想到的是我的新协议实现协议UIScrollViewDelegate.

protocol MyProtocol: UIScrollViewDelegate {
    var myVar: NSString { get }
    func myMethod()
}
Run Code Online (Sandbox Code Playgroud)

由于协议应具有其默认实现,因此我还为此协议创建了扩展.

extension MyProtocol {
    func myMethod() {
        print("I'm printing")
    }

    func scrollViewDidScroll(scrollView: UIScrollView) {
        print("I'm scrollin")
    }
}
Run Code Online (Sandbox Code Playgroud)

它编译,但不起作用.我做错了什么以及创建扩展协议的默认实现的正确方法是什么?

uiscrollviewdelegate ios swift swift-extensions swift-protocols

4
推荐指数
1
解决办法
3544
查看次数

如何创建几个缓存的UIColor

我的代码中有自定义颜色.我多次使用它们,我想只分配一次.

情况/问题

如果我们看一下UIColor标题,我们可以看到以下内容:

[...]

// Some convenience methods to create colors.  These colors will be as calibrated as possible.
// These colors are cached.

open class var black: UIColor { get } // 0.0 white

open class var darkGray: UIColor { get } // 0.333 white

[...]
Run Code Online (Sandbox Code Playgroud)

我创建了一个extensionUIColor,如下所示:

import UIKit

extension UIColor {

    class func colorWithHexString(_ hex: String) -> UIColor {

        print("\(#function): \(hex)")

        // some code, then it return a UIColor

        return UIColor(
            red: CGFloat((rgbValue & 0xFF0000) >> …
Run Code Online (Sandbox Code Playgroud)

uicolor swift swift-extensions

4
推荐指数
1
解决办法
567
查看次数

SKPhysicsBody的Swift便利初始化程序扩展

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)
    }

}
Run Code Online (Sandbox Code Playgroud)

我在运行时遇到了这个错误

-[PKPhysicsBody initWithRectangleOfSize:withAnchorPoint:]: unrecognized selector sent to instance 0x7f9b03c4fff0

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[PKPhysicsBody initWithRectangleOfSize:withAnchorPoint:]: unrecognized selector sent to instance 0x7f9b03c4fff0'
Run Code Online (Sandbox Code Playgroud)

这是我在代码中调用的方式

// redBox is a typical SKSpriteNode() …
Run Code Online (Sandbox Code Playgroud)

sprite-kit swift swift-extensions

3
推荐指数
1
解决办法
225
查看次数

斯威夫特:全球职能是不好的做法?

我读过全局变量很糟糕.但是,全球职能呢?假设我在我的iPhone应用程序项目中添加了一个名为Globals.swiftcontains 的文件func foo() { print("foo") }.这不是很糟糕,因为它可能与同名和签名的其他全局函数冲突吗?通过相同的论点,使用扩展来向现有类型添加新方法并不是一件坏事吗?

global swift swift-extensions

3
推荐指数
1
解决办法
1847
查看次数

在 Swift 中将整数转换为罗马数字字符串

我正在寻找IntegerSwift 并将其转换为罗马数字String。有任何想法吗?

nsnumber swift swift-extensions

3
推荐指数
1
解决办法
3986
查看次数

Swift - 仅当类符合使用方法调配的协议时才扩展类

我有一个名为 的协议NakedNavigationBar

\n\n

我还有一个扩展,可以扩展所有UIViewController符合NakedNavigationBar.

\n\n

问题是,在扩展中我想添加默认行为,以便在初始化时UIViewController,我们在UIViewController.

\n\n

这是我的协议和扩展:

\n\n
import UIKit\n\n\nprotocol NakedNavigationBar\n{\n\n}\n\nextension NakedNavigationBar where Self: UIViewController\n{\n    public override class func initialize()\n    {\n        struct Static\n        {\n            static var token: dispatch_once_t = 0\n        }\n\n        dispatch_once(&Static.token)\n        {\n            let originalSelector = #selector(viewWillAppear(_:))\n            let swizzledSelector = #selector(nakedNavigationBar_viewWillAppear(_:))\n\n            let originalMethod = class_getInstanceMethod(self, originalSelector)\n            let swizzledMethod = class_getInstanceMethod(self, swizzledSelector)\n\n            let didAddMethod = class_addMethod(self,\n                                               originalSelector,\n                                               method_getImplementation(swizzledMethod),\n                                               method_getTypeEncoding(swizzledMethod))\n\n            if didAddMethod\n            {\n                class_replaceMethod(self,\n                                    swizzledSelector,\n                                    method_getImplementation(originalMethod),\n                                    method_getTypeEncoding(originalMethod))\n            }\n            else\n            {\n                method_exchangeImplementations(originalMethod, …
Run Code Online (Sandbox Code Playgroud)

uiviewcontroller ios swift swift-extensions method-swizzling

3
推荐指数
1
解决办法
1108
查看次数

访问扩展中的结构时出现保护级别问题

我正在尝试将以下库实现到我的项目中:

https://github.com/knutigro/COBezierTableView

要使用它,可以为以下属性提供自定义值:

public extension UIView {

  public struct BezierPoints {

      static var p1 = CGPoint.zero
      static var p2 = CGPoint.zero
      static var p3 = CGPoint.zero
      static var p4 = CGPoint.zero
  }
}
Run Code Online (Sandbox Code Playgroud)

在我的MainVC中,这配置如下:

UIView.BezierPoints.p1 = CGPoint(...
UIView.BezierPoints.p2 = CGPoint(...
UIView.BezierPoints.p3 = CGPoint(...
UIView.BezierPoints.p4 = CGPoint(...
Run Code Online (Sandbox Code Playgroud)

在Swift 2.3演示项目中没有错误.在Swift 3项目中,我收到错误:

"由于'内部'保护级别,p1无法访问."

有人可以在这里解释一下这个问题,我猜测Swift 3有一些新的幕后权限接管需要被覆盖.

file-permissions access-control swift swift-extensions swift3

3
推荐指数
1
解决办法
5220
查看次数

为什么不直接扩展UIView或UIViewController?

我用这段代码看到了这个问题:

protocol Flashable {}

extension Flashable where Self: UIView 
{
    func flash() {
        UIView.animate(withDuration: 0.3, delay: 0, options: .curveEaseIn, animations: {
            self.alpha = 1.0 //Object fades in
        }) { (animationComplete) in
            if animationComplete == true {
                UIView.animate(withDuration: 0.3, delay: 2.0, options: .curveEaseOut, animations: {
                    self.alpha = 0.0 //Object fades out
                    }, completion: nil)
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

而且我想知道为什么我们不只是直接扩展UIView?或者在类似的情况下延伸UIViewController为什么用a扭转它where Self:

  • 是这样我们增加了意图,当其他开发人员来时,他们会看到这个类符合Flashable,Dimmable等吗?
  • 我们的UIView还有单独的有意义的扩展吗?而不是UIView或UIViewController的不同unNamed扩展?
  • POP有关于此主题的特定Apple指南吗?我见过开发人员这样做,但不知道为什么......

protocols swift swift-extensions protocol-oriented

3
推荐指数
1
解决办法
418
查看次数

访问UIColor扩展的成员时出错

我想将自定义颜色移动到UIColor的扩展名:

extension UIColor {
    static var nonSelectedTabColor: UIColor {
        return UIColor(white: 0.682, alpha: 1) // #AEAEAE
    }
}
Run Code Online (Sandbox Code Playgroud)

但是在尝试访问它时,它导致了我一个错误:

private static let defaultBorderColor = .nonSelectedTabColor
Run Code Online (Sandbox Code Playgroud)

Reference to member 'nonSelectedTabColor' cannot be resolved without a contextual type

这是什么问题?我怎样才能解决这个问题?

uicolor swift swift-extensions

3
推荐指数
1
解决办法
1806
查看次数

应用扩展和主应用之间共享文件

我需要您的帮助来弄清楚如何使主应用程序和应用程序扩展都可以访问我的 .json 文件。我有一个扩展,当按下发布按钮时,它会在 .json 文件中写入一些数据。但是当我尝试读取主应用程序中的文件时,主应用程序在错误的路径中读取一个空白文件,实际上应用程序在这个位置读取: https: //i.stack.imgur.com/idP3d.png和该扩展程序在此路径中写入一个文件:https://i.stack.imgur.com/v8yhr.png。我尝试将该文件包含在捆绑资源中,并为这两个应用程序设置文件的成员资格,但没有任何效果。要获取文件的路径,我使用以下方法:

let file = Bundle.main.url(forResource: "commonfile", withExtension: "json")
Run Code Online (Sandbox Code Playgroud)

非常感谢您的帮助

ios swift swift-extensions

3
推荐指数
1
解决办法
978
查看次数