美文网首页
Fundation框架(一)

Fundation框架(一)

作者: 随心_追梦 | 来源:发表于2018-03-14 08:25 被阅读0次

本文内容包括 NSNumber、NSString、NSArray、NSDictionary、NSSet

NSNumber

数字格式化用法

let number = 123456789
let numberObj = NSNumber(integerLiteral: number)

//1、设置数字格式化
let numberformatter = NumberFormatter()

//2、十进制数字
numberformatter.numberStyle = .decimal
var strNumber = numberformatter.string(from: numberObj)
print(strNumber!)  //123,456,789

//3、科学计数法
numberformatter.numberStyle = .scientific
strNumber = numberformatter.string(from: numberObj)
print(strNumber!)   //1.23456789E8

//4、百分数
numberformatter.numberStyle = .percent
strNumber = numberformatter.string(from: numberObj)
print(strNumber!)   //12,345,678,900%

//5、货币
numberformatter.numberStyle = .currency
strNumber = numberformatter.string(from: numberObj)
print(strNumber!)   //¥123,456,789.00

//6、大写数字
numberformatter.numberStyle = .spellOut
strNumber = numberformatter.string(from: numberObj)
print(strNumber!)   //一亿二千三百四十五万六千七百八十九

//7、设置本地化标识
numberformatter.locale = Locale(identifier: "en_US")
numberformatter.numberStyle = .currency
strNumber = numberformatter.string(from: numberObj)
print(strNumber!)   //$123,456,789.00
numberformatter.numberStyle = .spellOut
strNumber = numberformatter.string(from: numberObj)
print(strNumber!)   //one hundred twenty-three million four hundred fifty-six thousand seven hundred eighty-nine

NSNumber 与 swift 原生数字类型之间的桥接

let numberValue = 123

//基本数据类型转换为number
let numberObj1 = NSNumber(integerLiteral: numberValue)  //通过构造函数转换
let numberObj2 = numberValue as NSNumber                //桥接转换
//number转换为基本数据类型
let changeValue1 = numberObj1.intValue                  //通过方法转换
let changeValue2 = numberObj1 as? Int                   //桥接转换

NSString

不可变字符串基本用法

var str1 = "abcde"
var str2: NSString = "12345"
var str: NSString

//1、字符个数
print(str1.characters.count)    //5
print(str2.length)              //5

//2、创建一个新的字符串
str = NSString(string: str2)
print(str)   //12345

//3、拼接字符串
str = str2.appending(str2 as String) as NSString
print(str)  //1234512345

//4、判断字符串是否相等
if str.isEqual(to: str2 as String) {
    //相等
} else {
    //不相等
}

//5、比较字符串大小
let result: ComparisonResult = str.compare(str2 as String)
switch result {
case .orderedAscending:
    print("大于")
case .orderedSame:
    print("等于")
case .orderedDescending:
    print("小于")
}

//6、大写字符串
print(str1.uppercased())   //ABCDE

//7、小写字符串
print(str1.lowercased())   //abcde

//8、从下表1到3的位置截取字符串
var temp = str2.substring(from: 1) as NSString
let newStr = temp.substring(to: 2) as NSString
print(newStr)   //23

//9、字符串查找
var range = str2.range(of: "23")
if range.location == NSNotFound {
    //没有查找到字符串
} else {
    print("字符串的位置为:\(range.location), 长度为:\(range.length)")
    //字符串的位置为:1, 长度为:2
}

可变字符串基本用法

var str1: NSString = "Objective-C"
var str2: NSString
var replace: NSString
var mStr: NSMutableString
var range: NSRange

//1、从不可变字符串创建可变字符串
mStr = NSMutableString(string: str1)

//2、插入字符串
mStr.insert("java", at: mStr.length)
print(mStr)

//3、拼接字符串
mStr.append("C")
print(mStr)

//4、删除字符串
mStr.deleteCharacters(in: NSMakeRange(11, 5))
print(mStr)

//5、查找范围
range = mStr.range(of: "Obj")
print(range)

//6、替换字符串
mStr.replaceCharacters(in: NSMakeRange(1, 2), with: "sun")
print(mStr)

数组

数组与swift类型转换

var str = "a b c d"
var arr = str.components(separatedBy: " ")

//将原生数组转化为NSArray
let foundaryArr = arr as NSArray

//将NSAarray转换为swift类型数组
let swiftArr = foundaryArr as [AnyObject]

字典

字典与swift类型转换

var str1 = "a b c d"
var str2 = "1 2 3 4"

let keys = str1.components(separatedBy: " ")
let values = str2.components(separatedBy: " ")

let foundationDic = NSDictionary(objects: values, forKeys: keys as [NSCopying])

//将NSDictionary转换为swift原生数据类型 [NSObject: AnyObject]
let swiftDic = foundationDic as [NSObject: AnyObject]
print(swiftDic.description)

//将原生数据类型转化为NSDictionary
let dic = swiftDic as NSDictionary

NSSet

NSSet与swift类型转换

var nsset: NSSet = ["1", "2"]

//将NSSet 转化为 set
var swiftSet = nsset as Set<NSObject>
//将set转换为NSSet
var set = swiftSet as NSSet

相关文章

网友评论

      本文标题:Fundation框架(一)

      本文链接:https://www.haomeiwen.com/subject/wnvtqftx.html