标签: swift-protocols

在Swift中为UIViewController定制UIView

我使用代码来创建视图(带子视图),UIViewController这就是我的工作方式:

  1. 覆盖 loadView()

    class MYViewController: UIViewController {
    
    var myView: MyView! { return self.view as MyView }
    
       override func loadView() {
          view = MyView() 
       }
    }
    
    Run Code Online (Sandbox Code Playgroud)

以下是我创建自定义视图的方法:

class MyView: UIView {

    // MARK: Initialization

    override init (frame : CGRect) {
        super.init(frame : frame)
        addSubviews()
        setupLayout()
    }

    convenience init () {
        self.init(frame:CGRect.zero)
    }

    required init(coder aDecoder: NSCoder) {
        fatalError("This class does not support NSCoding")
    }

    // MARK: Build View hierarchy

    func addSubviews(){
        // add subviews
    }

    func setupLayout(){
        // Autolayout …
Run Code Online (Sandbox Code Playgroud)

design-patterns uiviewcontroller ios swift swift-protocols

5
推荐指数
1
解决办法
6432
查看次数

如何使协议关联类型需要协议继承而不是协议采用

在我的快速项目中,我有一种情况,我使用协议继承如下

protocol A : class{

}

protocol B : A{

}
Run Code Online (Sandbox Code Playgroud)

我接下来要实现的是声明另一个具有关联类型的协议,该类型必须从protocol继承A。如果我尝试将其声明为:

protocol AnotherProtocol{
    associatedtype Type : A
    weak var type : Type?{get set}
}
Run Code Online (Sandbox Code Playgroud)

它在编译时没有错误,但是AnotherProtocol在以下情况下尝试采用时:

class SomeClass : AnotherProtocol{

    typealias Type = B
    weak var type : Type?
}
Run Code Online (Sandbox Code Playgroud)

编译失败,SomeClass并声明与不一致的错误AnotherProtocol。如果我正确理解这一点,则意味着Im在尝试声明并询问如何声明从协议继承的关联类型时B 不采用?AA

我基于以下情况进行编译的事实做出了上述假设

class SomeDummyClass : B{

}

class SomeClass : AnotherProtocol{

    typealias Type = SomeDummyClass
    weak var type : Type?
}
Run Code Online (Sandbox Code Playgroud)

associated-types swift swift-protocols protocol-inheritance

5
推荐指数
1
解决办法
1288
查看次数

协议扩展,不符合协议

我正在创建一个名为contains的框架MyFramework,LoginProtocol.swift其中包含一些默认行为

import UIKit

public protocol LoginProtocol {
    func appBannerImage() -> UIImage?
    func appLogoImage() -> UIImage?
}


extension LoginProtocol {
    func appBannerImage() -> UIImage? {
        return (UIImage(named: "login_new_top")) 
    }

    func appLogoImage() -> UIImage? {
        return (UIImage(named: "appLogo"))

    }
}
Run Code Online (Sandbox Code Playgroud)

接下来,我添加一个新目标来创建一个名为的演示应用程序MyDemoApp,它正在使用MyFramework:

import UIKit
import MyFramework

class LoginViewContainer: UIViewController, LoginProtocol {    
    // I think I am fine with defaults method. But actually getting an error
}
Run Code Online (Sandbox Code Playgroud)

目前,我从编译器收到错误,如

type 'LoginViewContainer does not conform protocol 'LoginProtocol'
Run Code Online (Sandbox Code Playgroud)

我不知道为什么我收到此消息,因为通过协议扩展,该类不需要符合协议 …

protocols ios swift swift-protocols

5
推荐指数
1
解决办法
1741
查看次数

如何使struct和typealias符合@objc

是否可以使struct和/或typealias符合@objc?我希望创建可选的协议功能,一个返回a struct,另一个返回a typealias

public typealias SwiperData = (image: UIImage, title: String)

public struct SwiperPeekViewControllers{
  public var parentViewController: UIViewController!
  public var contentViewController: UIViewController!
  public init(parentVC: UIViewController, contentVC: UIViewController){
    parentViewController = parentVC
    contentViewController = contentVC
  }
}
Run Code Online (Sandbox Code Playgroud)

协议

    @objc public protocol SwiperPeekViewDelegate: class{
      func didUndoAction(index: Int, dataSource: SwiperData) 
// Method cannot be a member of an @objc protocol because the type of the parameter 2 cannot be represented in Objective-C
      func swiperPeekViewControllers()->SwiperPeekViewControllers
      func swiperPeekViewSize()->CGSize …
Run Code Online (Sandbox Code Playgroud)

swift swift-protocols

5
推荐指数
1
解决办法
3287
查看次数

指定实现协议的类

由于有2个协议P1P2,可以指定符合两国的协议,如类型:

typealias P = protocol<P1, P2>
Run Code Online (Sandbox Code Playgroud)

是否有类似的方式来指定类型的类型,并且也符合协议,例如像这样的东西(不起作用):

typealias P = UIView: P1
Run Code Online (Sandbox Code Playgroud)

swift swift-protocols

5
推荐指数
1
解决办法
186
查看次数

快速序列协议的Swift 2到3迁移

我正在尝试将以下代码从此库(https://github.com/dankogai/swift-json)转换为Swift 3兼容代码.

我很难弄清楚如何将Swift 2中使用的Sequence协议转换为Swift 3的正确版本.我找不到任何关于Swift 2 Sequence协议变化的文档与3相比.

这是我目前已尽可能转换为Swift 3的代码

extension JSON : Sequence {
    public func generate()->AnyIterator<(AnyObject,JSON)> {
        switch _value {
        case let o as NSArray:
            var i = -1
            return AnyIterator {
                i=i+1
                if i == o.count { return nil }
                return (i as AnyObject, JSON(o[i]))
            }
        case let o as NSDictionary:
            var ks = Array(o.allKeys.reversed())
            return AnyIterator {
                if ks.isEmpty { return nil }
                if let k = ks.removeLast() as? String {
                    return (k as AnyObject, …
Run Code Online (Sandbox Code Playgroud)

swift swift-protocols swift2 swift3 ios10

5
推荐指数
1
解决办法
699
查看次数

符合Swift中的Sequence和IteratorProtocol

我正在尝试编写自己的版本,IndexingIterator以加深我对的了解Sequence。我尚未在结构中将任何类型分配给关联类型迭代器。但是,编译器对此并没有抱怨,我得到了的默认实现makeIterator

以下是我的代码:

struct __IndexingIterator<Elements: IndexableBase>: Sequence, IteratorProtocol {
    mutating func next() -> Elements._Element? {
        return nil
    }
}
let iterator = __IndexingIterator<[String]>()
// this works and returns an instance of __IndexingIterator<Array<String>>. why?
iterator.makeIterator() 
Run Code Online (Sandbox Code Playgroud)

我认为必须有一些扩展Sequence可以添加默认实现。因此,我搜索了它Sequence.swift,才发现了它。

extension Sequence where Self.Iterator == Self, Self : IteratorProtocol {
  /// Returns an iterator over the elements of this sequence.
  public func makeIterator() -> Self {
    return self
  }
}
Run Code Online (Sandbox Code Playgroud)

我以为会是这样的:

extension Sequence where Self: IteratorProtocol …
Run Code Online (Sandbox Code Playgroud)

sequence swift swift-protocols swift3

5
推荐指数
1
解决办法
1641
查看次数

尝试在原型上调用函数时EXC_BAD_ACCESS

这是我遇到的一个有趣的快速问题.考虑以下类和协议:

class Person {

}

protocol Parent where Self: Person {
    func speak()
}

class GrandMotherPerson: Person, Parent {
    func speak() {
        print("I am a Grandmother Person")
    }
}

class GrandFatherPerson: Person, Parent {
    func speak() {
        print("I am a Grandfather Person")
    }
}

let gmp = GrandMotherPerson()
let gfp = GrandFatherPerson()
Run Code Online (Sandbox Code Playgroud)

现在你打电话的时候

gmp.speak() // Output: I am a Grandmother Person
gfp.speak() // Output: I am a Grandfather Person
Run Code Online (Sandbox Code Playgroud)

但是如果你转向父母

(gmp as Parent).speak() // EXC_BAD_ACCESS when it should say "I …
Run Code Online (Sandbox Code Playgroud)

swift swift-protocols

5
推荐指数
1
解决办法
398
查看次数

使用默认参数值声明协议功能

我希望此功能在协议中:

func slideToRight(currentViewController viewController: UIViewController, completion: ((Bool)->())? = nil) {
// do some stuff
}
Run Code Online (Sandbox Code Playgroud)

但是当我写这样的协议时:

protocol SomeDelegate { 
func slideToRight(currentViewController viewController: UIViewController, completion: ((Bool)->())? = nil) 
}
Run Code Online (Sandbox Code Playgroud)

我收到一个错误:

协议方法中不允许使用默认参数

我知道,我可以这样定义签名:

protocol SomeDelegate { 
func slideToRight(currentViewController viewController: UIViewController, completion: ((Bool)->())?) 
}
Run Code Online (Sandbox Code Playgroud)

但是然后,我将无法调用缺少“ completion”字样的函数:

slideToRight(currentViewController viewController: vc)
Run Code Online (Sandbox Code Playgroud)

callback optional swift completion swift-protocols

5
推荐指数
1
解决办法
2070
查看次数

如何约束协议类型的参数以符合Swift 4中的AnyObject?

以一个非常接近在下面看一下:

// Note that this protocol can only be applied to reference types.
protocol Ref: class {
  var zibbles: Int { get set }
}

class Reference: Ref {
  var zibbles: Int = 42
}

// Note very carefully that we are NOT passing an
// instance, but a type itself.
func thwip<T: AnyObject>(into target: T.Type) {

}

// This compiles.
thwip(into: Reference.self)

// This fails to compile.
thwip(into: Ref.self) 
Run Code Online (Sandbox Code Playgroud)

无论情况多么罕见,这是语言应该能够完成的事情.编译器知道Ref必须符合的任何实例AnyObject,因此类型约束thwip应该可以工作,但事实并非如此.

请注意,如果我们 …

swift swift-protocols

5
推荐指数
0
解决办法
104
查看次数