Swift

39.Swift学习之常用数据类型

2020-12-20  本文已影响0人  YungFan

除了基础语法,在 iOS 开发中,经常还会用到一些数据类型如日期,二进制数据等。

日期

let date = Date()
print(date)  // 输出:2019-08-01 02:27:14 +0000
let date = Date()
// 构造格式化工具
let formatter = DateFormatter()
// 指定输出格式,可以自由制定,但是固定的是 yyyy:年 MM:月 dd:日 hh:时 mm:分 ss:秒
// 如果想要24小时制,hh改为HH
formatter.dateFormat = "yyyy-MM-dd hh:mm:ss"

// format.dateFormat = "yyyy年-MM月-dd日"
// Date转String
let dateStr = formatter.string(from: date)
print(dateStr)
let formatter = DateFormatter()
// 默认英文,设置中文
formatter.locale = Locale(identifier: "zh-CN")
// 相对日期
formatter.doesRelativeDateFormatting = true
// dateStyle与timeStyle有五种样式
formatter.dateStyle = .medium
formatter.timeStyle = .short

let dateStr = formatter.string(from: Date())
print(dateStr) // 今天 下午8:00
let formatter = ISO8601DateFormatter()
// String转Date
let date = formatter.date(from: "2020-01-01T12:00:00+08:00")
// Date转String
let dateStr = formatter.string(from: Date())
let formatter = RelativeDateTimeFormatter()
// 默认英文,设置中文
formatter.locale = Locale(identifier: "zh-CN")

// 默认显示风格 numeric
formatter.localizedString(from: DateComponents(year: -1)) // "1年前"
formatter.localizedString(from: DateComponents(month: -1)) // "1个月前"
formatter.localizedString(from: DateComponents(day: -1)) // "1天前"
formatter.localizedString(from: DateComponents(hour: 10)) // "10小时后"
formatter.localizedString(from: DateComponents(minute: 10)) // "10分钟后"
formatter.localizedString(from: DateComponents(second: -10)) // "10秒钟前"

// 设置显示风格 named
formatter.dateTimeStyle = .named
formatter.localizedString(from: DateComponents(year: -1)) // "去年"
formatter.localizedString(from: DateComponents(month: -1)) // "上个月"
formatter.localizedString(from: DateComponents(day: -1)) // "昨天"
formatter.localizedString(from: DateComponents(hour: 10)) // "10小时后"
formatter.localizedString(from: DateComponents(minute: 10)) // "10分钟后"
formatter.localizedString(from: DateComponents(second: -10)) // "10秒钟前"

日历

// 获取日历
let calendar = Calendar.current
var date = Date()
// 创建一个DateComponents
var components = DateComponents()
// 创建一个Calendar
let calendar = Calendar.current
// 拆分年月日时分秒与星期
calendar.component(.year, from: date)
calendar.component(.month, from: date)
calendar.component(.day, from: date)
calendar.component(.hour, from: date)
calendar.component(.minute, from: date)
calendar.component(.second, from: date)
calendar.component(.weekday, from: date)
// 创建一个DateComponents
var components = DateComponents()
// 设置各个时间成分
components.year = 2019
components.month = 8
components.day = 1
components.hour = 12
components.minute = 0
components.second = 0
// 创建一个Calendar
let calendar = Calendar.current

// 构造日期
var date = calendar.date(from: components)
print(date!)
let date = Date()
// 判断一个日期是否是今天
let isToday = Calendar.current.isDateInToday(date)
// 判断一个日期是否是昨天
let isYesterday = Calendar.current.isDateInToday(date)
// 判断一个日期是否是明天
let isTomorrow = Calendar.current.isDateInTomorrow(date)
// 判断一个日期是否是属于周末双休日
let isWeekend = Calendar.current.isDateInWeekend(date)
// 判断一个日期和另外一个是否相等
let isSameDay = Calendar.current.isDate(date, inSameDayAs: Date())

// 比较两个日期 
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy/MM/dd"

let date1 = dateFormatter.date(from: "2020/1/1")
let date2 = dateFormatter.date(from: "2021/12/31")
let components: Set<Calendar.Component> = [.year, .month, .day]
let delta = NSCalendar.current.dateComponents(components, from: date1!, to: date2!)
let timeDifference = "\(delta.year!)" + "年" + "\(delta.month!)" + "个月" + "\(delta.day!)" + "天"

二进制数据

let originStr = "Hello Swift Data"
// String转Data
let data: Data = originStr.data(using: .utf8)!
// Data转String
let str = String(data: data, encoding: .utf8)
print(str)
// 引入Compression模块
import Compression

let image = UIImage(named: "largeImage")
let imgFile: Data = image!.pngData()!

do {
    let compressed1 = try NSData(data: imgFile).compressed(using: .lzfse)
    let compressed2 = try NSData(data: imgFile).compressed(using: .lz4)
    let compressed3 = try NSData(data: imgFile).compressed(using: .lzma)
    let compressed4 = try NSData(data: imgFile).compressed(using: .zlib)

} catch {
    print(error.localizedDescription)
}
上一篇下一篇

猜你喜欢

热点阅读