在Swift 3.0中NSMutableSet的componentsByString

时间:2022-09-25 18:00:20

I have label and NSMutable Set.

我有标签和NSMutable Set。

I want to assign my set value to label.text.

我想将我的设置值分配给label.text。

  @IBOutlet var location: UILabel!
 var mutSet:NSMutableSet = NSMutableSet()

        self.location.text = **mutSet.allObjects.componetsJoinedByString("\n")**

mutSet.allObjects.componetsJoinedByString("\n") throws compile time error similarly I have tried joined , it also throws compile time error.

mutSet.allObjects.componetsJoinedByString(“\ n”)抛出编译时错误同样我尝试加入,它也抛出编译时错误。

Please provide me alternative way in Swift

请在Swift中提供替代方法

1 个解决方案

#1


1  

First of all to answer your question: You have to write

首先回答你的问题:你必须写

(mutSet.allObjects as! [String]).joined(separator: "\n")

NS(Mutable)Set lacks any type information. You have to cast the type to actual [String]

NS(Mutable)Set缺少任何类型信息。你必须将类型转换为实际的[String]


It's highly recommended to use native Set and generally you are discouraged from using NSMutable... types if there is a native counterpart.

强烈建议使用本机Set,如果有本机对应物,通常不鼓励使用NSMutable ...类型。

var mutSet = Set<String>(["a", "b", "c"])

You can append (actually insert) an item - a set is unordered

您可以追加(实际插入)一个项目 - 一个集合是无序的

mutSet.insert("d")

or an array of items

或一系列项目

mutSet.formUnion(["c", "d", "f"])

Joining is much shorter than in Foundation NSMutableSet

加入比基础NSMutableSet短得多

mutSet.joined(separator: "\n")

or sorted joined

或分类加入

mutSet.sorted().joined(separator: "\n")

#1


1  

First of all to answer your question: You have to write

首先回答你的问题:你必须写

(mutSet.allObjects as! [String]).joined(separator: "\n")

NS(Mutable)Set lacks any type information. You have to cast the type to actual [String]

NS(Mutable)Set缺少任何类型信息。你必须将类型转换为实际的[String]


It's highly recommended to use native Set and generally you are discouraged from using NSMutable... types if there is a native counterpart.

强烈建议使用本机Set,如果有本机对应物,通常不鼓励使用NSMutable ...类型。

var mutSet = Set<String>(["a", "b", "c"])

You can append (actually insert) an item - a set is unordered

您可以追加(实际插入)一个项目 - 一个集合是无序的

mutSet.insert("d")

or an array of items

或一系列项目

mutSet.formUnion(["c", "d", "f"])

Joining is much shorter than in Foundation NSMutableSet

加入比基础NSMutableSet短得多

mutSet.joined(separator: "\n")

or sorted joined

或分类加入

mutSet.sorted().joined(separator: "\n")