Objective-C Simplest way to create comma separated string from an array of objects

后端 未结 4 865
既然无缘
既然无缘 2021-01-30 19:49

So I have a nsmutablearray with a bunch of objects in it. I want to create a comma separated string of the id value of each object.

4条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-01-30 20:23

    Use the NSArray instance method componentsJoinedByString:.

    In Objective-C:

    - (NSString *)componentsJoinedByString:(NSString *)separator
    

    In Swift:

    func componentsJoinedByString(separator: String) -> String
    

    Example:

    In Objective-C:

    NSString *joinedComponents = [array componentsJoinedByString:@","];
    

    In Swift:

    let joinedComponents = array.joined(seperator: ",")
    

提交回复
热议问题