捕获自我从未使用过(Capture self was never used)

编程入门 行业动态 更新时间:2024-10-15 04:27:00
捕获自我从未使用过(Capture self was never used)

我不发布整个代码,因为它真的太大了。 无论如何,它是以这种方式构建的用户注册。

有一个模态,其子节点是pageViewController:

第一页是登录; 第二页是注册;

从登录您可以到达注册,一旦注册,pageViewController应自动转换到第一页,以便用户可以使用他的新凭据登录。

用户注册的过程由按钮和Alamofire管理:一旦点击按钮,用户在文本域中插入的值被验证,然后我开始发布请求,将数据发送到服务器并在一段时间内接收JSON数据。

这很简单(抱歉说得太多),但最后,收到JSON之后,这里发生了一些奇怪的事情:

let j = JSON as! NSDictionary let userStatus = j.object(forKey: "status")! if((userStatus as! Int) == 0){ // print("user registerd") let alert = UIAlertController(title: "Registration ok", message: "Registration OK", preferredStyle:UIAlertControllerStyle.alert) let okAlert = UIAlertAction(title:"OK", style: .default, handler: {action in v.scrollToPreviousViewController() }) alert.addAction(okAlert) self.present(alert, animated: true, completion: nil)

该代码应该做什么? 用户单击警报按钮后,pageviewcontroller应返回动画并返回登录屏幕。

怎么了? 它返回那里但没有动画。

这使我认为我应该避免“污染”保留给GUI的全局线程。 事实上,我试图将所有代码放在:

DispatchQueue.main.async { [unowned self] in

没有任何成功。

然后,我尝试了另一件事:

let okAlert = UIAlertAction(title:"OK", style: .default, handler: {action in DispatchQueue.main.async { [unowned self] in v.scrollToPreviousViewController() }})

我不完全理解为什么,但这实际上有效并消除了动画的问题。

但那unowned self产生一个警告:“捕捉自我从未被使用过”。 我究竟做错了什么?

I don't post the entire code because it's really too large. Anyway, it's a user registration built this way.

There is a modal whose child is a pageViewController:

the first page is for Login; the second page is for Registration;

From Login you can reach Registration and, once registered, pageViewController should automatically transition to the first page, so that the user can login with his new credentials.

The process of user registration is managed by a button and Alamofire: once the button is clicked, the values the user inserted in the textfields are validated, then I start a post request, sending data to the server and receiving JSON data back in a while.

It's anything very simple (sorry for speaking too much), but in the end, after I receive the JSON, something strange happens here:

let j = JSON as! NSDictionary let userStatus = j.object(forKey: "status")! if((userStatus as! Int) == 0){ // print("user registerd") let alert = UIAlertController(title: "Registration ok", message: "Registration OK", preferredStyle:UIAlertControllerStyle.alert) let okAlert = UIAlertAction(title:"OK", style: .default, handler: {action in v.scrollToPreviousViewController() }) alert.addAction(okAlert) self.present(alert, animated: true, completion: nil)

What is supposed to do this code? Once the user clicks the alert button, the pageviewcontroller should return with an animation to the login screen.

What happens? It returns back there but without animation.

This led me to think I should avoid "polluting" the global thread reserved to the GUI. And, in fact, I tried to put all the code inside a:

DispatchQueue.main.async { [unowned self] in

without any success.

Then, I tried another thing:

let okAlert = UIAlertAction(title:"OK", style: .default, handler: {action in DispatchQueue.main.async { [unowned self] in v.scrollToPreviousViewController() }})

I don't perfectly understand why, but this practically works and eliminates the issue with the animation.

But that unowned self there generates a warning: "Capture self was never used". What am I doing wrong?

最满意答案

如果您没有在异步块内使用self ,那么您将明显收到此警告。 请尝试使用_而不是self 。

If you didn't use self inside the async block then you will get this warning obviously. Please try with _ instead of self.

更多推荐

本文发布于:2023-08-07 17:51:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1465381.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:使用过   自我   Capture

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!