小编meo*_*eow的帖子

如果我使用单例网络服务发出网络请求,是否需要使用 [weak self]?

假设我有一个使用 Alamofire 的 SessionManager 的网络单例,例如:

进口阿拉莫火

class Network {
    static let shared = Network()
    private init() {}

    private var sessionManager: SessionManager = {
        let configuration = URLSessionConfiguration.default
            configuration.httpAdditionalHeaders = SessionManager.defaultHTTPHeaders

                return SessionManager(configuration: configuration)
    }

    func postRequest(params: [String: Any]? = nil, completion: (() -> ())? = nil) {
        sessionManager.request(url, method: .post, parameters: params).validate().responseData {
            // do something with response
            completion()
        }?      }
}
Run Code Online (Sandbox Code Playgroud)

然后我在服务类中使用它:

class SomeService {
    static let shared = SomeService()
    private init() {}

    func doSomePostRequest(params: [String: Any]? = nil, …
Run Code Online (Sandbox Code Playgroud)

ios swift alamofire

6
推荐指数
1
解决办法
765
查看次数

使用闭包参数分配闭包

假设我有这门课

class ClosureTest{
    var nestedClosure: (((String) -> Void) -> Void)?
}
Run Code Online (Sandbox Code Playgroud)

如何给 赋值nestedClosure

我尝试了下面的代码,但出现错误。有人可以帮忙解释一下吗?

let cTest = ClosureTest()
cTest.nestedClosure = {{ myStr -> Void in } -> Void }
Run Code Online (Sandbox Code Playgroud)

closures swift

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

为什么@state 在警报解除后被重置

在这里完成 SwiftUI 初学者。我正在查看这个示例,并试图了解@state变量的生命周期。

showingAlert初始化为falsetrue在点击按钮时设置为。我无法理解的部分是为什么它会false在警报解除时重置回?我没有将其设置为false 任何地方。

我希望它会留下来 true

@State private var showingAlert = false

var body: some View {
   Button(action: { self.showingAlert = true }
   ) { 
     Text("Show Alert")
   }
   .alert(isPresented: $showingAlert) {
     Alert(title: Text("Important message"))
   }
}
Run Code Online (Sandbox Code Playgroud)

ios swift swiftui

2
推荐指数
1
解决办法
71
查看次数

请求失败:forbidden twitterkit禁止(403)

我正在尝试使用适用于iOS的TwitterKit API提取列表成员.据我了解,这样做将使我能够"客户验证",绕过我个人消费者和消费者密钥的需要.

下面的代码尝试使用此REST端点获取列表的成员

let client = TWTRAPIClient()
let endpoint = "https://api.twitter.com/1.1/lists/members.json"
let params = ["owner_screen_name" : "palafo", "slug" : "breakingnews"]
var clientError : NSError?

let request = client.URLRequestWithMethod("GET", URL: endpoint, parameters: params, error: &clientError)

client.sendTwitterRequest(request) { (response, data, connectionError) -> Void in
   if connectionError != nil {
       print("Error: \(connectionError)")
   }

   do {
       let json = try NSJSONSerialization.JSONObjectWithData(data!, options: [])
       print("json: \(json)")
   } catch let jsonError as NSError {
       print("json error: \(jsonError.localizedDescription)")
   }
}
Run Code Online (Sandbox Code Playgroud)

运行,这给了我这个错误:

Error Domain=TwitterAPIErrorDomain Code=220 "Request …
Run Code Online (Sandbox Code Playgroud)

ios twitter-fabric

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

编程约束 - 使用布局锚覆盖整个超级视图

是否可以使用布局锚来覆盖整个超级视图的屏幕?

使用 NSLayoutConstraints,我可以使用下面的代码实现这种效果

dimmingView = UIView()
dimmingView.translatesAutoresizingMaskIntoConstraints = false
dimmingView.backgroundColor = UIColor(white: 0.0, alpha: 0.5)

containerView?.addSubview(dimmingView)

NSLayoutConstraint(item: containerView!, attribute: .leadingMargin, relatedBy: .equal, toItem: dimmingView, attribute: .leadingMargin, multiplier: 1, constant: 0).isActive = true
NSLayoutConstraint(item: containerView!, attribute: .trailingMargin, relatedBy: .equal, toItem: dimmingView, attribute: .trailingMargin, multiplier: 1, constant: 0).isActive = true
NSLayoutConstraint(item: containerView!, attribute: .topMargin, relatedBy: .equal, toItem: dimmingView, attribute: .topMargin, multiplier: 1, constant: 0).isActive = true
NSLayoutConstraint(item: containerView!, attribute: .bottomMargin, relatedBy: .equal, toItem: dimmingView, attribute: .bottomMargin, multiplier: 1, constant: 0).isActive = true …
Run Code Online (Sandbox Code Playgroud)

ios nslayoutconstraint swift nslayoutanchor

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