草庐IT

gravity_value_t

全部标签

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

iOS swift : Value of type 'NotificationCenter' has no member 'publisher'

我正在尝试将新的Combine框架与NotificationCenter一起使用,正如Apple在这段视频中所解释的那样:https://developer.apple.com/videos/play/wwdc2019/721/您可以在幻灯片21中找到它。看来我的项目没有读取Combineframeworkapi。importCombineimportFoundationlettrickNamePublisher=NotificationCenter.default.publisher(for:.newTrickDownloaded)我收到这个错误:“NotificationCente

swift - "Initialisation of immutable value' 上下文 ' was never used, consider replacing assignment to ' _' 或删除它

我在swift中声明了一个变量letcontext:LAContext=LAContext()发出警告"Initialisationofimmutablevalue'context'wasneverused,considerreplacingassignmentto'_'orremovingit. 最佳答案 都在错误信息里value...wasneverused您的变量未在任何地方使用,因此Xcode告诉您可以删除它(因为拥有未使用的变量会浪费内存)。只需在某处使用您的变量,错误就会消失(例如,从中获取一个值,打印它等)。当然是指在

ios - 索引到函数数组 : Expression resolves to an unused l-value

我正在尝试对函数数组进行索引,但出现错误:“表达式解析为未使用的左值”。我试图用谷歌搜索这意味着什么,但信息很少,而且我发现的似乎无关。有谁知道我在这里做错了什么?任何帮助将非常感激!谢谢。overridefunctableView(tableView:UITableView,cellForRowAtIndexPathindexPath:NSIndexPath)->UITableViewCell{varcell:UITableViewCell=myTableView.dequeueReusableCellWithIdentifier("cell")asUITableViewCellva