如何从Swift中的Closure返回值?(How to return value from a Closure in Swift?)
所以我在我的应用程序中使用了fabric插件/ Twitter-kit来使用twitter API。 我想获取名人个人资料图片的图片网址。 这里是我的代码如下。
func getImageURL(celebrity :String) -> String{ var imageURL = "" let client = TWTRAPIClient() let statusesShowEndpoint = userSearch let params = ["q": celebrity, "page" : "1", "count" : "1" ] var clientError : NSError? let request = client.URLRequestWithMethod("GET", URL: statusesShowEndpoint, 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)") let profileImageURL: String? = json[0].valueForKey("profile_image_url_https") as? String imageURL = self.cropTheUrlOfImage(profileImageURL!) print(imageURL) //how do i return the value back. confused ????? return imageURL } catch let jsonError as NSError { print("json error: \(jsonError.localizedDescription)") } } return imageURL }
我不知道如何返回值,因为方法在闭包完全执行之前完成。 我是新来的迅速,任何帮助表示赞赏。
我想在tableview的cellForRowIndexPath中使用这个方法来下载imageURl中的图像。
So I am using fabric plugin/Twitter-kit to use twitter api in my application. I want to get the image URL of profile picture of a celebrity. here is my code below.
func getImageURL(celebrity :String) -> String{ var imageURL = "" let client = TWTRAPIClient() let statusesShowEndpoint = userSearch let params = ["q": celebrity, "page" : "1", "count" : "1" ] var clientError : NSError? let request = client.URLRequestWithMethod("GET", URL: statusesShowEndpoint, 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)") let profileImageURL: String? = json[0].valueForKey("profile_image_url_https") as? String imageURL = self.cropTheUrlOfImage(profileImageURL!) print(imageURL) //how do i return the value back. confused ????? return imageURL } catch let jsonError as NSError { print("json error: \(jsonError.localizedDescription)") } } return imageURL }
I don't know how to return value because the method finishes before the closure executes completely. I am new to swift, Any help is appreciated.
I want to use this method in cellForRowIndexPath in tableview to dowload image from imageURl.
原文:https://stackoverflow.com/questions/38296910
最满意答案
我能够通过等待条件来解决问题,该等待条件是timedout直到联系人字段有数据,然后得到联系公司。 它需要几秒钟,但至少它解决了标签错误
I was able to solve the problem with an wait condition that timedout until the contact field had data, and then got the company of the contact. It takes a few seconds extra, but at least it solved the label error
相关问答
更多-
如果要确保在单行文本字段中输入的值是GUID,则需要在字段的create和update事件上创建同步插件,如果该值不是GUID,则抛出异常。 您还可以使用工作流和自定义活动执行此操作。 除了此插件/工作流之外,您还可以编写附加到字段的OnChange事件的JavaScript,以便在CRM Web UI更新字段时强制执行检查。 If you want to ensure that the value entered inside a single line of text field is a GUID, ...
-
Dynamics CRM - 更改查找(自动完成)客户实体视图(Dynamics CRM - Change Lookup (Autocomplete) View of Customer Entity)[2024-02-06]
您正在寻找的是查找视图 。 每个实体都有一个快速查找视图,可以通过添加过滤条件等在一定程度上进行自定义。要对过滤条件进行更细粒度的级别控制,可以使用预搜索事件 What you are looking for is the Lookup View. Each entity has a quick find view which can be customized to an extent by adding a filter criteria etc. To have more granular level ... -
不,您必须先知道您尝试检索哪个实体。 将Customer字段中保存的值作为EntityReference : var customer = entity.GetAttributeValue
("customerid"); 获取EntityReference的LogicalName : var customerEntity = customer.LogicalName; No, you must first know which entity you are trying ... -
首先,如果要保存更改,还需要使用UpdateObject : c.new_caseage = numDays; xrm.UpdateObject(c); xrm.SaveChanges(); 第二,你不能更新已关闭的案例,你需要先重新打开记录才能更新。 first you need to use also UpdateObject if you want to save your changes: c.new_caseage = numDays; xrm.UpdateObject(c); xrm.Save ...
-
Dynamics CRM - 案例实体上客户字段内的文本不会更新(Dynamics CRM - Text inside customer field on case entity won't update)[2024-01-09]
我能够通过等待条件来解决问题,该等待条件是timedout直到联系人字段有数据,然后得到联系公司。 它需要几秒钟,但至少它解决了标签错误 I was able to solve the problem with an wait condition that timedout until the contact field had data, and then got the company of the contact. It takes a few seconds extra, but at least ... -
我认为你有一个错字。 案件实体的名称不是incident I think you have a typo. The name of the case entity is incident not incedent
-
要创建帐户,您可以使用下面给出的代码。 单击按钮调用函数CreateAccount() function CreateAccount() { var accountId, accountName; var context = Xrm.Page.context; var serverUrl = context.getServerUrl(); var ODATA_ENDPOINT = "/XRMServices/2011/OrganizationData.svc"; ...
-
我一直在与这个完全相同的问题作斗争,并且发现了这篇文章。 我以为我会为你发布解决方案(如果你还需要它)以及将来遇到这个问题的任何人。 这是我到达的解决方案: - 使用插件注册工具在适当的步骤注册新图像(Stage =“40”,MessageName =“DeliverIncoming”) - 将新图像设置为后期图像 - 在您的插件中获取Post Image的实体ID: Guid emailID = context.PostEntityImages["PostImage"].Id; Entity emailF ...
-
Dynamics CRM的点击通话功能(如Lync / Skype)(Click-To-Call feature for Dynamics CRM (like Lync/Skype))[2023-11-27]
目前,我找到了两种可用于实现自定义CTC功能的选项。 (两者都有不受动态crm官方支持的缺点。) 全球功能区 非常简单:将“Click-To-Call”按钮添加到全局功能区,该功能区仅在选择一行时在特定网格上启用。 此按钮指的是JS-Action,它通过ODATA检索电话号码,然后启动拨号过程。 Global Ribbon CustomRule注入 向功能区添加一个全局按钮,该按钮引用每个的JS资源。 然后,JScript将链接中的所有操作与.ms-crm-Phone类解除绑定,并替 ... -
MS Dynamics CRM 2011:我可以复制实体并重命名实体吗?(MS Dynamics CRM 2011: Can I Copy an Entity and Rename the Entity?)[2022-03-14]
您可以创建和导出仅包含要复制的实体的非托管解决方案。 在您需要手动编辑解决方案xml并更改相关条目后(现在我不确切知道哪一个,但我认为只有实体名称加上其他东西)。 导回这个非托管解决方案后,您将获得新实体。 You can create and export an unmanaged solution containing only the entity you want to duplicate. After you need to manually edit the solution xml and c ...