草庐IT

fisrt_value

全部标签

ios - swift 2 : Cannot subscript a value of type '[GKScore]?'

让highscore=leaderboardRequest.scores[0]为!GKScore结果为无法下标“[GKScore]”类型的值?Swift2中正确的语法是什么 最佳答案 leaderboardRequest.scores未解包,使用下标前解包。ifletscores=leaderboardRequest.scores{iflethighscore=scores.firstas?GKScore{//safelyuse`highscore`here}}“展开”是Swift中的一个关键概念,它与Optionals一起出现。[

ios - swift 3.1 : Cannot convert value of type '(_) -> ()' error/Problems with closures

我升级到Swift3.1,我收到了一些新错误,这些错误似乎是3.1语法问题,因为在迁移之前它们不是问题。它们大多与闭包有关,如本例所示:letalert=UIAlertController(title:"Success",message:"Thanksforparticipatinginourraffle!",preferredStyle:UIAlertControllerStyle.alert)alert.addAction(UIAlertAction(title:"OK",style:.default,handler:{performSegue(withIdentifier:"to

objective-c - fatal error : unexpectedly found nil while unwrapping an Optional value, 快速获取 json 错误

获取json错误当我尝试返回json时,出现错误fatalerror:unwrappedlyfoundnilwhileunwrappinganOptionalvalue:funcgetJson(str:NSString)->AnyObject{varproxiesURL=NSURL(string:str)varproxiesDataJson=NSData.dataWithContentsOfURL(proxiesURL,options:NSDataReadingOptions.DataReadingUncached,error:nil)varjson:AnyObject!if(prox

ios - swift/Objective-C : How to get value from object by string name

classA{varx=1}vara=A()如何使用字符串名称(a["x"])从对象“a”获取变量“x”? 最佳答案 如果类继承自NSObject,这将起作用,您可以在其中使用valueForKey:获取属性。importFoundationclassA:NSObject{varx=1}leta=A()letaval=a.valueForKey("x")println("\(aval)")请注意aval在这里是一个AnyObject?因为没有类型信息。您需要自己转换或测试它是什么。 关于

ios - CLLocationManager fatal error : unexpectedly found nil while unwrapping an Optional value Swift

我有一个问题,因为当我尝试获取位置用户并在我的.plist中获得权限时,我的应用程序崩溃了,这是我的代码。importUIKitimportMapKitimportCoreLocationclassmapClass:UIViewController,MKMapViewDelegate,CLLocationManagerDelegate{varlocation4=CLLocation()varlm=CLLocationManager()@IBOutletvarpropMapa:MKMapView!lm.delegate=selflm.desiredAccuracy=kCLLocation

swift - 索引(: Int) method of Range in swift return incorrect value

在半开放范围对象上使用index(of:Int)方法时,如果范围不是从0开始,它总是返回不正确的值。请参见下面的代码。letrange=(3..我不明白为什么会产生这样的结果。谁能解释一下? 最佳答案 索引是不透明的对象。如果它不是数组,则不应假定它们是从零开始的,甚至不应假定它们是整数(有关示例,请参见String.Index)。要获取从零开始的整数索引,您需要获取与startIndex的距离:letrange=(3..但是,对于Int范围,它基本上与以下内容相同:letintegerIndex=5-range.lowerOffs

ios - Swift 通用变量 fatal error : unexpectedly found nil while unwrapping an Optional value

我正在尝试实现一个简单的通用方法:funcfindMax(numbers_array:[T])->(min_tuple:T,max_tuple:T){varmax_number:T?varmin_number:T?foriinnumbers_array{if(max_numberi){min_number=i}}return(min_number!,max_number!)}我正在尝试访问这样的方法:letresult=findMax([3.4,5,-7,-7.8])但是每当我运行这段代码时,我都会收到以下错误:fatalerror:unexpectedlyfoundnilwhileu

ios - 无法分配类型为 '(String!, Bool, [AnyObject]!, NSError!)->Void to a value of type UIActivityViewControllerCompletionWithItemsHandler?' 的值

我的项目中有以下几行代码...@IBActionfuncshareMeme(sender:UIBarButtonItem){letnewMeme=save()letmemedImage=newMeme.memedImageletactivityViewController=UIActivityViewController(activityItems:[memedImage],applicationActivities:nil)presentViewController(activityViewController,animated:true,completion:nil)activit

ios - Swift: "This class is not key value coding-compliant..."用于 Double 值

我有一个场景,我在Swift中有一个简单的数据对象,其中包含多个属性变量。它们是String?和Double?值类型的混合。我正在尝试使用valueForKey检索每个属性的值。我的代码看起来像这样......letmyDataObj=...letstringKeyName="myStringProperty"letdoubleKeyName="myDoubleProperty"guardletstringPropertyValue=myDataObj.valueForKey(stringKeyName)else{return}guardletdoublePropertyValue=m

ios - swift 3 : expression pattern of type 'Int' cannot match values of type 'UnsafeMutableRawPointer'

我正在将一个应用程序迁移到Swift3,但Xcode对此函数抛出错误:错误是在casecondition("contentSize",MyObservationContext)我这样做是为了更新uiwebview的内容大小varMyObservationContext=0overridefuncobserveValue(forKeyPathkeyPath:String?,ofobject:Any?,change:[NSKeyValueChangeKey:Any]?,context:UnsafeMutableRawPointer?){guardletkeyPath=keyPathelse