草庐IT

index-async

全部标签

swift - 距离(从 :to:)' is unavailable: Any String view index conversion can fail in Swift 4; please unwrap the optional indices

我试图将我的应用程序迁移到Swift4、Xcode9。我收到此错误。它来自第3方框架。distance(from:to:)'isunavailable:AnyStringviewindexconversioncanfailinSwift4;pleaseunwraptheoptionalindicesfuncnsRange(fromrange:Range)->NSRange{letutf16view=self.utf16letfrom=range.lowerBound.samePosition(in:utf16view)letto=range.upperBound.samePositio

ios - 在 NSAttributedString API 中将 Range<Index> 与 NSRange 一起使用

我正在尝试确定给定字符串在String中出现的索引,然后生成一个NSRange使用这些索引将属性添加到NSMutableAttributedString.问题是rangeOfString返回Range但是addAttributes:range:期望一个NSRange.我尝试创建一个NSRange来自Range的开始和结束索引失败了,因为String.CharacterView.Index不是Int,因此它不会编译。如何使用Range创建NSRange的值?varoriginalString="Hello{world}andthosewhoinhabitit."letfirstBrace

swift - 在 Swift 中使用 Dispatch_Async 更新 UI

在我的代码中,我有一个简单的for循环,它循环100次并嵌套for循环以产生延迟。延迟之后,我正在通过dispatch_async更新UI中的进度View元素。但是,我无法更新UI。有谁知道为什么UI不更新?注意:下面的打印语句用于验证for循环是否正确循环。foriin0.. 最佳答案 三个观察,两个基本的,一个更高级一点:除非循环本身在另一个线程上运行,否则您的循环将无法更新该主线程中的UI。因此,您可以将其分派(dispatch)到某个后台队列。在Swift3中:DispatchQueue.global(qos:.utilit

swift - 无法将类型 'NSRange'(又名 '_NSRange')的值转换为预期参数类型 'Range<Index>'(又名 'Range<String.CharacterView.Index>')

我的代码中有一个错误,例如“无法将类型‘NSRange’(又名‘_NSRange’)的值转换为预期的参数类型‘Range’(又名‘Range’)”,但我不知道如何解决这个问题请任何人帮助我?我在这里发布我的代码。NSUserDefaults.standardUserDefaults().setObject("HeyIhavestartedusingthisChatApp",forKey:"Status")varstrNumber:String=txtPhoneNumber.text!varmyRange:NSRange=NSMakeRange(0,2)varmyRange1:NSRan

ios - 无法将类型 'NSRange'(又名 '_NSRange')的值转换为预期类型 'Range<Index>'(又名 'Range<String.CharacterView.Index>')

检查字符串字符的范围时出现此错误...@objcfunctextField(textField:UITextField,shouldChangeCharactersInRangerange:NSRange,replacementStringstring:String)->Bool{letshouldChange=falselettext=textField.textvarnewString=text!.stringByReplacingCharactersInRange(range,withString:string)as?NSStringifnewString.length>14{n

swift - array[index] 不返回可选值有什么原因吗?

我觉得array[index]应该返回一个可选值,因为索引可能超出范围。这将允许这样的代码:ifletobject=array[index]{//Dostuff}使用扩展方法很容易做到这一点,但最好知道出现这种情况的真正原因。 最佳答案 这是myfirstSwiftradars之一,它被关闭为“行为正确”。还讨论了inthedevforums.正如DaveAbrahams所指出的:Astorationale,It’seasyandverycommontohavestaticknowledgethattheindexisinrange

Swift 3.0 遍历 String.Index 范围

Swift2.2可以实现以下功能:letm="alpha"foriinm.startIndex..对于3.0,我们得到以下错误:Type'Range'(aka'Range')doesnotconformtoprotocol'Sequence'我想在swift中对字符串执行一个非常简单的操作——简单地遍历字符串的前半部分(或者更一般的问题:遍历一个字符串的范围)。我可以做以下事情:lets="string"varmidIndex=s.index(s.startIndex,offsetBy:s.characters.count/2)letr=Range(s.startIndex..但这里我

ios - 警告 : UICollectionViewFlowLayout has cached frame mismatch for index path 'abc'

这是导致警告的代码:privateoverridefunclayoutAttributesForItemAtIndexPath(indexPath:NSIndexPath)->UICollectionViewLayoutAttributes?{letattributes=super.layoutAttributesForItemAtIndexPath(indexPath)letdistance=CGRectGetMidX(attributes!.frame)-self.midX;vartransform=CATransform3DIdentity;transform=CATransfo

ios - 如何检查 indexPath 是否有效,从而避免 "attempt to scroll to invalid index path"错误?

如何检查indexPath是否有效?我想滚动到indexPath,但如果我的UICollectionViewsubview未完成加载,我有时会收到错误消息。 最佳答案 你可以检查-numberOfSections-numberOfItemsInSection:UICollectionViewDataSource以查看您的indexPath是否有效。例如extensionUICollectionView{funcisValid(indexPath:IndexPath)->Bool{guardindexPath.section

grand-central-dispatch - 对自己使用 dispatch_async

我在将Objective-C代码移植到Swift时遇到过几次这个问题。假设我有以下代码:dispatch_async(dispatch_get_main_queue()){self.hostViewController?.view.addSubview(self.commandField)}这将导致错误,强调整个dispatch_async调用,提供:Couldnotfindmember'addSubview'我认为这是一个尚未正确实现的错误,因为如果我将addSubview调用放在dispatch_asyncblock之外,项目构建良好。最初我认为它可能与在block中捕获self有