在带有 SIOSocket 的 Swift 闭包中使用 self
Using self in a Swift closure with SIOSocket
我希望有人能帮助我。我试图将 SIOSocket 与 Swift 项目一起使用。
我正在使用 https://github.com/MegaBits/SIOSocket/issues/30 which seems to work, but i want to be able to declare a socket
as a var
like the Objective-C project example at https://github.com/MegaBits/WorldPin 中的示例。所以我可以在代码中的其他地方使用它来调用 emit。
我假设我不理解 Obj-C 块和 Swift 闭包基础知识以及 self
的使用或需要将 var
声明为块但不能似乎把我的头缠绕在它周围。任何帮助都感激不尽。
Objective-C代码:
@property SIOSocket *socket;
[SIOSocket socketWithHost: @"http://localhost:3000" response: ^(SIOSocket *socket)
{
self.socket = socket; //I Want to do this in swift
__weak typeof(self) weakSelf = self;
self.socket.onConnect = ^()
{
weakSelf.socketIsConnected = YES;
[weakSelf mapView: weakSelf.mapView didUpdateUserLocation: weakSelf.mapView.userLocation];
};
[self.socket on: @"join" callback: ^(SIOParameterArray *args)
{
[weakSelf mapView: weakSelf.mapView didUpdateUserLocation: weakSelf.mapView.userLocation];
}];
[self.socket on: @"update" callback: ^(SIOParameterArray *args)
{
NSString *pinData = [args firstObject];
等等……
Swift代码:
private func connectToHost() {
SIOSocket.socketWithHost(host, reconnectAutomatically: true, attemptLimit: 0, withDelay: 1, maximumDelay: 5, timeout: 20, response: {
socket in
self.socket = socket // This gives me a use of unresolved identifier self error
socket.onConnect = {
println("Connected to \(host)")
socket.emit("add user", args: [username])
}
socket.on("login", callback: {(AnyObject data) -> Void in
println(["login": data])
socket.emit("new message", args: [message])
})
socket.onDisconnect = {
println("Disconnected from \(host)")
}
})
}
尝试将其更改为:
SIOSocket.socketWithHost(host, reconnectAutomatically: true, attemptLimit: 0, withDelay: 1, maximumDelay: 5, timeout: 20, response: {(socket: SIOSocket) in
self.socket = socket // This gives me a use of unresolved identifier self error
//...
})
我把socket in
改成了(socket: SIOSocket) in
您的代码应该可以工作,确保您拥有所有正确的类型和可选类型,通过使用 var
而不是 let
定义 socket 来确保它是一个 read-write
变量.
尝试使用 let response: (SIOSocket) -> Void = {...}
.
在方法调用之前定义闭包
我希望有人能帮助我。我试图将 SIOSocket 与 Swift 项目一起使用。
我正在使用 https://github.com/MegaBits/SIOSocket/issues/30 which seems to work, but i want to be able to declare a socket
as a var
like the Objective-C project example at https://github.com/MegaBits/WorldPin 中的示例。所以我可以在代码中的其他地方使用它来调用 emit。
我假设我不理解 Obj-C 块和 Swift 闭包基础知识以及 self
的使用或需要将 var
声明为块但不能似乎把我的头缠绕在它周围。任何帮助都感激不尽。
Objective-C代码:
@property SIOSocket *socket;
[SIOSocket socketWithHost: @"http://localhost:3000" response: ^(SIOSocket *socket)
{
self.socket = socket; //I Want to do this in swift
__weak typeof(self) weakSelf = self;
self.socket.onConnect = ^()
{
weakSelf.socketIsConnected = YES;
[weakSelf mapView: weakSelf.mapView didUpdateUserLocation: weakSelf.mapView.userLocation];
};
[self.socket on: @"join" callback: ^(SIOParameterArray *args)
{
[weakSelf mapView: weakSelf.mapView didUpdateUserLocation: weakSelf.mapView.userLocation];
}];
[self.socket on: @"update" callback: ^(SIOParameterArray *args)
{
NSString *pinData = [args firstObject];
等等……
Swift代码:
private func connectToHost() {
SIOSocket.socketWithHost(host, reconnectAutomatically: true, attemptLimit: 0, withDelay: 1, maximumDelay: 5, timeout: 20, response: {
socket in
self.socket = socket // This gives me a use of unresolved identifier self error
socket.onConnect = {
println("Connected to \(host)")
socket.emit("add user", args: [username])
}
socket.on("login", callback: {(AnyObject data) -> Void in
println(["login": data])
socket.emit("new message", args: [message])
})
socket.onDisconnect = {
println("Disconnected from \(host)")
}
})
}
尝试将其更改为:
SIOSocket.socketWithHost(host, reconnectAutomatically: true, attemptLimit: 0, withDelay: 1, maximumDelay: 5, timeout: 20, response: {(socket: SIOSocket) in
self.socket = socket // This gives me a use of unresolved identifier self error
//...
})
我把socket in
改成了(socket: SIOSocket) in
您的代码应该可以工作,确保您拥有所有正确的类型和可选类型,通过使用 var
而不是 let
定义 socket 来确保它是一个 read-write
变量.
尝试使用 let response: (SIOSocket) -> Void = {...}
.