草庐IT

LCD_write_chinese_string

全部标签

ios - Swift 将数据映射到 [String : Any]

我想将数据类型转换为[String:Any],但JSONSerialization告诉我:Cannotforceunwrapvalueofnon-optionaltype'Data'varjson:[String:Any]do{letjsonEncoder=JSONEncoder()letencodedJson=tryjsonEncoder.encode(message)json=tryJSONSerialization.data(withJSONObject:encodedJson!,options:[])as?[String:Any]}catch{log.error(error.l

iOS swift : write print and debug prints into a file

晚上,是否可以将所有打印和调试打印保存在一个文件中?即使我的应用程序未从Xcode启动,我也想要记录其执行的操作。我想覆盖print和debugPrint方法并将输入写入文件。谢谢 最佳答案 Swift标准库中有方法:funcprint(_items:Any...,separator:String=default,terminator:String=default,tooutput:inoutTarget)whereTarget:TextOutputStream和funcdebugPrint(_items:Any...,separa

swift - 无法将类型 '[String]?' 的值转换为预期的参数类型 'String'

我想要做的是将firstCard的图像设置为与名称对应于firstCardString的图像文件相同。例如,在下面的例子中,代码可以设置self.firstCard.image来显示随机选择的名为“card1”的图像(我已经剪掉了其余部分为简洁起见,整个数组包含52个对象)。vardeckArray=["card1":["Bear","Ball"],"card2":["Bear","Ball"],"card3":["Bear","Ball"],"card4":["Bear","Ball"],"card5":["Bear","Ball"],"card6":["Bear","Ball"],

string - Swift:子字符串函数的 String.Index 测试边界

哇。Swift使从简单字符串复制子字符串的操作变得真正繁琐。大多数编程语言都允许根据字符在字符串中的整数位置简单地对字符进行索引,从而使定位字符或范围成为简单的数学问题。因为Swift允许使用具有不同位深度的各种字符,所以必须首先找到每个字符的精确(内存?)索引,基于它从字符串的开头或结尾的位置。然后可以将这些位置传递给String类的方法,该方法返回范围内的子字符串。我写了一个函数来完成这项工作://arguments:Theparentstring,numberofcharsfrom1stcharinitandtotalcharlengthofsubstringfuncsubStr

string - Swift String 常量的类型是否与 String 文字不同?

在Swift2.1中,下面的代码片段会产生错误。varstr="Hello,playground!"//SuccessCaseif"!"==str.characters.last{print("Toneitdownplease")}//FailCaseletbang="!"ifbang==str.characters.last{//thislinewon'tcompileprint("Toneitdownplease")}编译器错误说:Binaryoperator'=='cannotbeappliedtooperandsoftype'String'and'_Element?'那么在这种

string - 无法将类型 'Int' 的值转换为预期的参数类型 'Index'(又名 'String.CharacterView.Index')

代码:letx:String=("abc".substringFromIndex(1))print(x)//functail(s:String)->String{//returns.substringFromIndex(1)//}//print(tail("abcd"))这按预期工作。但是如果我取消注释最后4行,那么我会得到:Error:cannotconvertvalueoftype'Int'toexpectedargumenttype'Index'(aka'String.CharacterView.Index')真的很奇怪。 最佳答案

swift - 如何比较 Range<String.Index> 和 DefaultBidirectionalIndices<String.CharacterView>?

此比较在Swift2中有效,但在Swift3中无效:让myStringContainsOnlyOneCharacter=mySting.rangeOfComposedCharacterSequence(at:myString.startIndex)==mySting.characters.indices如何比较Range和DefaultBidirectionalIndices? 最佳答案 来自SE-0065–ANewModelforCollectionsandIndicesInSwift2,collection.indicesret

swift - 从 String 到 UnsafePointer<Int8> 的最佳转换是什么?

要将String转换为UnsafePointer,我使用这个:vartail=(""asNSString).utf8String但是有没有什么方法可以不用NSString强制转换呢?我在C-library方法中使用UnsafePointer。 最佳答案 有专门的方法可以做到这一点:.withCString(_:):yourString.withCString{pointerin//workwiththepointerreturnresult}或者,如果您希望将其作为属性(property),则有.utf8CString:vartai

swift 5 : Index of a Character in String

在Swift5之前,我有这个扩展工作:fileprivateextensionString{funcindexOf(char:Character)->Int?{returnfirstIndex(of:char)?.encodedOffset}}现在,我收到一条已弃用的消息:'encodedOffset'isdeprecated:encodedOffsethasbeendeprecatedasmostcommonusageisincorrect.Use`utf16Offset(in:)`toachievethesamebehavior.有没有比使用utf16Offset(in:)更简单的

string - Swift 是否支持字符串 block ?

Ruby、Python等语言都支持字符串block。如:a='''HelloWorld'''Swift支持字符串block吗? 最佳答案 没有。如Swift编程语言介绍的词法结构中所述,stringliterals包含在一对引号中,并且不得包含换行符。语法规定:string-literal→"quoted-text"quoted-text→quoted-text-itemquoted-textoptquoted-text-item→escaped-characterquoted-text-item→(expression)quote