var numbers = "Hello,Goodbye,Hi,Bye"
var numbersArr = numbers.componentsSeparatedByString(",")
Run Code Online (Sandbox Code Playgroud)
//["Hello"."Goodbye","Hi","Bye"]
以上是我正在尝试做的基本表示.我正在尝试使用componentsSeparatedByString()逗号将字符串拆分为数组,其中数组的每个组件都位于原始字符串的每个逗号之间.
我正在使用IBM Swift Sandbox(抱歉,我在Windows上:)),在Swift 3.0中,我收到此错误消息:
value of type 'String' has no member 'componentsSeparatedByString'
Run Code Online (Sandbox Code Playgroud)
我知道Swift 3相当新,这就是为什么我找不到这个错误的任何其他参考.
所以我正在从 Xcode 导出一个 .txt 文件,但是每当我尝试导出它时,比如通过电子邮件,它会创建一封没有附件和空消息的电子邮件。当我尝试将其导出到 iCloud Drive 时,白屏出现一秒钟,然后离开。当我检查 iCloud Drive 时,该文件不存在。这里有什么问题?
let message = testLabel.text
func getDocumentsDirectory() -> NSString {
let paths = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)
let documentsDirectory = paths[0]
return documentsDirectory as NSString
}
let filePath = getDocumentsDirectory().appendingPathComponent("matrixFile.txt")
do{
try message?.write(toFile: filePath, atomically: true, encoding: String.Encoding.utf8)
}catch let error as NSError{
testLabel.text = String(describing: error)
}
let documentInteractionController = UIDocumentInteractionController()
documentInteractionController.url = NSURL.fileURL(withPath: filePath)
documentInteractionController.uti = "public.data, public.content"
documentInteractionController.name = "matrixFile"
documentInteractionController.presentOptionsMenu(from: view.frame, in: view, animated: true)
Run Code Online (Sandbox Code Playgroud) 这是我的文本文件导出和导入难题的第二部分。现在,我有写代码和导出代码。难题的最后一部分是导入代码和读取代码。我想从电子邮件中提取附件并将其导入到应用程序中,以便我可以阅读。我有基本的阅读代码:
@IBAction func readButton(_ sender: UIButton) {
let filePath = getDocumentsDirectory().appendingPathComponent("matrixFile.txt")
do {
try displayField.text = String(contentsOfFile: path!,
encoding: String.Encoding.utf8)
} catch let error as NSError{
displayField.text = String(describing: error)
}
}
Run Code Online (Sandbox Code Playgroud)
但是我也想知道导入时是什么filePath。我调整了info.plist的导出功能,但是否需要对此做更多更改?这是我当前添加的内容:
提前致谢。
我需要在swift中乘以2个矩阵,所以为了使用加速,我需要2个类型为double的数组.问题是,我需要乘法的两个数组是类型UInt32.无论如何将UInt32数组转换为双数组?
var UInt32TestArray: [UInt32] = [1,4,2,3]
var Int32TestArray: [Double] = [Double](UInt32) //Doesn't work
Run Code Online (Sandbox Code Playgroud)