xcode 转换[字符串]?Swift 中的字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28607315/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 06:40:57  来源:igfitidea点击:

Convert [String]? to String in Swift

arraysxcodestringswift

提问by Wenjie Zhong

For my project, I extracted tweets from a CSV file in Swift. Problem is now all tweets are parsed as one element in an array, separated by ",".

对于我的项目,我从 Swift 中的 CSV 文件中提取了推文。问题是现在所有的推文都被解析为一个数组中的一个元素,用“,”分隔。

let tweetsOfColumns = columns["tweet"]
let seperatedColumns = tweetsOfColumns.componentsSeparatedByString(",")

Error message: '[String]?' does not have a member named 'componentsSeparatedByString'.

错误消息:“[字符串]?” 没有名为“componentsSeparatedByString”的成员。

I checked if tweetsOfColumns contains multiple elements, but it doesn't allow me to subscript with tweetsOfColumns[index].

我检查了 tweetsOfColumns 是否包含多个元素,但它不允许我使用 tweetsOfColumns[index] 下标。

回答by Dániel Nagy

When you try to get an element from a dictionary, like

当您尝试从字典中获取元素时,例如

columns["tweet"]

it will give you back an optional, because if there is nothing associated with the key, it gives you back nil (None), otherwise the value wrapped in an optional (Some(data)). So you have to unwrap the optional for example:

它会给你一个可选的,因为如果没有与键相关的任何东西,它会给你零(无),否则值包装在一个可选的(一些(数据))中。所以你必须打开可选的,例如:

columns["tweet"]!

回答by David Berry

Looking at the link you reference, columns["tweets"]is going to give you back an array of the values from the "tweets" column, so it's what you need already, there's no additional comma's to split things on, you just need:

查看您引用的链接,columns["tweets"]将返回“推文”列中的一组值,因此它已经是您所需要的,没有额外的逗号来拆分内容,您只需要:

let seperatedColumns = columns["tweet"]

to have an array containing the tweet column for each row.

有一个包含每行推文列的数组。

回答by Christian

You have to either use the optional ?to access the string:

您必须使用可选?来访问字符串:

let seperatedColumns = tweetsOfColumns?.componentsSeparatedByString(",")

But you should unwrap it:

但你应该打开它:

if let unwrappedTweets = tweetsOfColumns?.componentsSeparatedByString(","){
    let seperatedColumns = unwrappedTweets
}

回答by Simon

The problem is probably that you'll get an optional back, which you have to unwrap. And the easiest and most elegant is to use the if-let unwrapper.

问题可能是你会得到一个可选的返回,你必须打开它。最简单、最优雅的是使用 if-let 解包器。

if let tweetsOfColumns = columns["tweet"] {
    let seperatedColumns = tweetsOfColumns.componentsSeparatedByString(",")
    // do something with the seperatedColumns
}

回答by Price Ringo

Based on David's question and the OP's response in the OP comments, you can use map on the Array returned by columns["tweet"]. Please post actual data/code in the future.

根据大卫的问题和 OP 评论中 OP 的回应,您可以在由 column["tweet"] 返回的数组上使用 map。请在未来发布实际数据/代码。

let columns = [
    "tweet":["handleX,tag1,tag2,textA,textB",
             "handleY,tag1,tag2,textC,textD"]]

var chunk = [[String]]()
if columns["tweet"] != nil {
    chunk = columns["tweet"]!.map {
        return ##代码##.componentsSeparatedByString(",")
    }
}

enter image description here

在此处输入图片说明