草庐IT

ios - Swift:比较隐式展开的可选结果为 "unexpectedly found nil while unwrapping an Optional values"

如果得到这个类并且想根据值比较它的实例classLocationOption:NSObject{varname:String!varradius:Int!varvalue:String!overridefuncisEqual(object:AnyObject?)->Bool{ifletotherOption=objectas?LocationOption{return(self.name==otherOption.name)&&(self.radius==otherOption.radius)&&(self.value==otherOption.value)}returnfalse}}

ios - 为什么我会收到此错误 : Result values in '? :' expression have mismatching types '[String]?' and 'String'

所以我是swift的新手,我正在尝试使用谷歌地图。我无法弄清楚为什么这条线有效:让state=p?.administrativeArea!=nil?p?.administrativeArea:"nil6"但不是这一行:让areaOfInterest=p?.areasOfInterest!=nil?p?.areasOfInterest:"nil7"我在areaOfInterest行收到此错误消息:'?中的结果值:'表达式有不匹配的类型'[String]?'和“字符串”提前致谢。CLGeocoder().reverseGeocodeLocation(userLocation){(place

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

swift 2 : Is there any way to use 'default' in the switch statement of enum with associated values?

我有一个递归枚举,其中大多数情况具有相同类型的关联值:indirectenumLocation{caseTitle(String?)caseRegion(Location)caseArea(Location,Location)caseCity(Location,Location)caseSettlement(Location,Location)caseStreet(Location,Location)caseHouse(Location,Location)}我想做的是形成一个漂亮的字符串描述,其中将包括所有非零标题。funcgetStringFromLocation(location:

swift : switch case multiples values from dictionary keys

我想为多个值做一个switchcase,其中这些值是从字典的键中获取的。myDict=["dog":"waf","cat":"meaow","cow":"meuh"]letanimal="cat"switchanimal{casemyDict.keys:print(myDict[animal])case"lion":print("toodangerous!")}default:print("unknownanimal")}如何获取myDict键并将它们转换为元组(或其他))?我尝试了Array(myDict.keys)但它失败了:Expressionpatternoftype'Arra

string - fatal error : high- and low-surrogate code points are not valid Unicode scalar values

这个问题在这里已经有了答案:HowcanIgeneratearandomunicodecharacterinSwift?(2个答案)关闭7年前。有时在用57292之类的值初始化UnicodeScalar时会产生以下错误:fatalerror:high-andlow-surrogatecodepointsarenotvalidUnicodescalarvalues这是什么错误,为什么会发生,以后我该如何预防?

ios - 为什么字典中的 "values"属性不是一个简单的数组,而是一些奇怪的东西?

letdict=[1:"One",2:"Two",3:"Three"]letvalues=dict.valuesprint(values.dynamicType)打印:LazyMapCollection,String>这里有两件事我不明白。如果values返回一个Array会不会更简单一些?另外,什么是LazyMapCollection?我调查了Apple'sreference但它实际上没有提供任何信息(或者我无法理解的任何信息)。您可以迭代此对象,因为它是CollectionType:forvinvalues{print(v)}打印:TwoThreeOne但由于某些原因Apple没有

swift 4 : pattern match an object against a tuple (Tuple pattern cannot match values of the non-tuple type)

我有一个包含几个字段的自定义结构,我想在快速switch语句中对其进行模式匹配,这样我就可以通过将其中一个字段与另一个字段进行比较来自定义匹配正则表达式。例如鉴于这种结构:structMyStruct{letheader:Stringlettext:String}我喜欢像这样进行模式匹配:switch(someInstance){case("h1","[a-z]+"):...case("h1","0-9+"):...}我尝试使用如下模式匹配函数让它工作:func~=(pattern:(String,String),value:MyStruct)->Bool{returnvalue.he

swift - Realm swift : always put nil values last in sort

我正在使用Swift2.2编写一个针对iOS8并使用Realm的应用程序。我允许用户使用Results.sorted(_:ascending:)根据各种可选属性对对象进行排序。这对于降序排序非常有效,但对于升序排序,nil值放在第一位,这看起来不正确。许多数据库系统都有一个NULLSFIRST/LAST选项,使用CoreData,看起来有可能subclassNSSortDescriptor.在Realm中排序时,有没有办法始终将nil值放在最后?即使只有一个hacky策略,也将不胜感激。 最佳答案 除了Results.sorted(

ios - Swift 可解码 : how to transform one of values during decoding?

默认情况下,Decodable协议(protocol)将JSON值转换为对象值而无需更改。但有时你需要在json解码期间转换值,例如,在JSON中你得到{id="id10"}但在你的类实例中你需要输入数字10进入属性id(或者甚至进入具有不同名称的属性)。您可以实现方法init(from:),您可以在其中使用任何值执行您想要的操作,例如:publicrequiredinit(fromdecoder:Decoder)throws{letcontainer=trydecoder.container(keyedBy:CodingKeys.self)latitude=trycontainer.