I want to pass a Bool value from on view controller to another without the help of segues. So i referred & got Delegates.
I have applied delegates in my App. But the Delegate method is not being called. I don't know where i am making the mistake.
So Please help me.
MainViewController
class MainViewController: UIViewController, WriteValueBackDelegate {
@IBOutlet weak var LoginButton: UIButton!
var LoggedInL :Bool?
override func viewDidLoad() {
super.viewDidLoad()
}
func writeValueBack(value: Bool) {
println("Delegate Method")
if (value …Run Code Online (Sandbox Code Playgroud) 我正在构建一个简单的状态引擎,我想要一个可以在其中移动的状态集合。
我想要解决这个问题的方法是枚举可能的状态,这些状态也定义了表示该状态的相应类,这样如果我决定转移到该状态,我就可以动态地构造该状态。
在下面的代码中,我尝试构建一个运行良好的 State 对象的枚举。我遇到困难的是,如何访问此枚举的值作为我可以从中调用静态构造函数方法的类型?在下面的代码中,我得到的错误是尝试使用枚举值调用 moveToState 并不代表 StartupStates 类型,这似乎......
所以问题确实是,为什么这不起作用,或者我可以通过什么其他方式拥有类类型和/或类级别(静态)方法的枚举来调用构造函数?
public enum StartupStates<State> {
case Start(StartState)
case DownloadFiles(DownloadFilesState)
}
public protocol State {
var stateEngine : StateEngine {get set}
}
public class StateEngine
{
var currentState : State?
public func moveToState(newState : StartupStates<State>)
{
}
}
public class StartState : BaseState
{
func doStateTasks()
{
// Move to next state, downloading files
// ERROR IS HERE:
// "Cannot convert file of type '(DownloadFileState)->StartupStates<...>' to expected argument type 'StartupStates<State>'"
stateEngine.moveToState(StartupStates.DownloadFiles)
} …Run Code Online (Sandbox Code Playgroud) 我有一个自定义类型Banana,我想创建一个Array(或者,如果必须的话,Sequence) of的扩展Banana以符合协议,CustomStringConvertible以便调用descriptionof 数组Banana将返回“一堆香蕉”。这是可能的,如果是这样,我将如何去做?
仍然是一个 Swift 菜鸟,我一直在寻找一种正确的方法/最佳实践来管理我的UITableView(使用自定义UserCells)中的行删除,基于在using 委托中点击 a UIButton,UserCell这似乎是最干净的方法。
我跟着这个例子:UITableViewCell Buttons with action
我拥有的
UserCell 类
protocol UserCellDelegate {
func didPressButton(_ tag: Int)
}
class UserCell: UITableViewCell {
var delegate: UserCellDelegate?
let addButton: UIButton = {
let button = UIButton(type: .system)
button.setTitle("Add +", for: .normal)
button.addTarget(self, action: #selector(buttonPressed), for: .touchUpInside)
button.translatesAutoresizingMaskIntoConstraints = false
return button
}()
override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
super.init(style: .subtitle, reuseIdentifier: reuseIdentifier)
addSubview(addButton)
addButton.rightAnchor.constraint(equalTo: self.rightAnchor, constant: -6).isActive = true
addButton.centerYAnchor.constraint(equalTo: self.centerYAnchor).isActive …Run Code Online (Sandbox Code Playgroud) 我喜欢检查给定的值是否Any.Type符合 Swift 中的协议。似乎@objc可以通过调用来检查基于的协议class_conformsToProtocol(),但我不知道如何使用纯 swift 协议进行检查。
// ObjC
@objc protocol MyObjcProtocol {
}
class MyObjcClass: NSObject, MyObjcProtocol {
}
class_conformsToProtocol(MyObjcClass.self, MyObjcProtocol.self) // true
// Swift
protocol MySwiftProtocol: AnyObject {
}
class MySwiftClass: MySwiftProtocol {
}
class_conformsToProtocol(MySwiftClass.self, MySwiftProtocol.self) // error
Run Code Online (Sandbox Code Playgroud)
如果是实例的情况,我可以检查if let object = object as? MySwiftProtocol { ... }方法的类型,但Any.Type不可能是这种情况。
谢谢...
我正在尝试在Swift中实现一个Functor,并且发现我无法为返回类型的泛型map函数指定不同的关联类型.请在我错的地方提出建议.
这是我的代码:
protocol Functor {
associatedtype T
func map<U>(_ transform: (T) -> U) -> Self // should return `Self` with associated type U
}
enum Result<A>: Functor {
typealias T = A
case success(A)
case failure(Error)
func map<U>(_ transform: (A) -> U) -> Result<A> { // autocompletion sets return type as `Result<A>` instead of Result<U>
switch self {
case let .success(value):
return .success(transform(value))
default:
return self
}
}
}
Run Code Online (Sandbox Code Playgroud) 我对 Swift 还是很陌生,虽然我已经阅读了 Apple 的文档以及许多关于此的主题和线程,但我仍然无法理解{ get }和{ get set }. 我的意思是,我正在寻找一个具体例子的解释。
比如,例如:
protocol PersonProtocol {
var firstName: String { get }
var lastName: String { get set }
}
Run Code Online (Sandbox Code Playgroud)
这两个属性之间的实际区别是什么?我试图在操场上玩这些属性:
struct Person: PersonProtocol {
var firstName: String
var lastName: String
}
var p = Person(firstName: "John", lastName: "Lennon")
print(p.firstName) // John
print(p.lastName) // Lennon
p.firstName = "Paul"
p.lastName = "McCartney"
print(p.firstName) // Paul
print(p.lastName) // McCartney
Run Code Online (Sandbox Code Playgroud)
没有帮助...感谢您的帮助。
当协议将属性声明为optional并且具体类型将其声明为non-optional 时,如何使具体类型符合协议?
这是问题所在:
protocol Track {
var trackNumber: Int? { get } // not all tracks have a track number
}
struct SpotifyTrack {
let trackNumber: Int // all SpotifyTrack are guaranteed to have a track number
}
extension SpotifyTrack: Track {
var trackNumber: Int? {
return self.trackNumber // WARNING: All paths through this function will call itself
}
}
Run Code Online (Sandbox Code Playgroud)
我不想将其设为trackNumber可选项,SpotifyTrack因为我知道 SpotifyTracks 的价值将始终存在。有没有比重命名属性更优雅的解决方案?
在阅读有关异常的 swift 论坛时,我发现了一个有趣的问题。关于异常的例子之一是这样的:
protocol Base {
func foo() throws -> Int
}
protocol Refined: Base {
func foo() -> Int
}
struct Test: Refined {
func foo() -> Int {
0
}
}
Run Code Online (Sandbox Code Playgroud)
有趣的是,我认为它不会编译是错字,但它确实如此。我不确定这在幕后是如何工作的。我的意思是当协议采用另一个协议时,它也采用了它的要求。但在这种情况下,声明相同的方法而不抛出某种方式也满足第一个协议Base。
至少我预计Test需要有 2 个foo. 我在这里缺少什么?
swift ×10
swift-protocols ×10
ios ×2
arrays ×1
delegates ×1
enums ×1
singleton ×1
swift3 ×1
uitableview ×1