Swift项目中的小tips

最近开发的项目使用的是swift语言,在强大的项目工期压力下,推着自己不断的摸索swift,以下为自己摸索过程中的一下小tips:

字符串转数组

let str = "1,2,3,4,5"
let arr = str.components(separatedBy: ",")

数组转字符串(源码注释解释)

public func joined(separator: String = default) -> String

/// Returns a new string by concatenating the elements of the sequence,
/// adding the given separator between each element.
///
/// The following example shows how an array of strings can be joined to a
/// single, comma-separated string:
///
///     let cast = ["Vivien", "Marlon", "Kim", "Karl"]
///     let list = cast.joined(separator: ", ")
///     print(list)
///     // Prints "Vivien, Marlon, Kim, Karl"
///
/// - Parameter separator: A string to insert between each of the elements
///   in this sequence. The default separator is an empty string.
/// - Returns: A single, concatenated string.

CGFloat输出控制

String(format: "%.2f", 1.9999999)

通过字符串查找控制器,并且进行跳转

let nameSpace = Bundle.main.infoDictionary!["CFBundleExecutable"] as! String
let Claz = NSClassFromString(nameSpace + "." + "controllerName") as! UIViewController.Type
let vc = Claz.init()
navigationController?.pushViewController(vc, animated: true)

不定时更新ing~

你可能感兴趣的:(Swift项目中的小tips)