结构体的介绍
1.结构体(struct)是由一系列相同类型或者不同类型的数据构成的集合2.结构体指的是一种数据结构3.结构体是值类型,在方法中传递时时值传递4.OC 结构体 只能有属性 Swift 不但可以有属性还可以拥有自己的方法
定义结构体
struct Location {
var x : Double
var y : Double
}
创建结构体对应的值
var center =
Location(x: 20, y: 30)
创建系统结构体方式
let rect = CGRect(x:
0, y:
0, width:
100, height:
100)
let size = CGSize(width:
100, height:
100)
let point = CGPoint(x:
50, y:
50)
let range = NSRange(location:
3, length:
5)
给结构体扩充方法
struct Location {
var x : Double
var y : Double
func test() {
print(
"结构体中的函数");
}
mutating
func moveH(disyance : Double) {
self.x += disyance
}
}
给机构体扩充构造函数
1.默认情况下,系统会为每一个结构体提供一个默认的构造函数,并且该构造函数,要求给每一个成员属性进行赋值2.构造函数都是以 init 开头,并且构造函数不需要返回值3.在构造函数结束时,必须保证所有的成员属性都被初始化
struct Location {
var x : Double
var y : Double
init(x:Double,y:Double) {
self.x = x
self.y = y
}
init(xyStr:String) {
let arr = xyStr.components(separatedBy:
",")
let x = arr[
0]
let y = arr[
1]
self.x = Double(x) ??
0
self.y = Double(y) ??
0
}
}
创建&&输出
下面是一个通过定义几个 Struct 获取 App 信息 、设备信息的工具类。
import Foundation
import UIKit
import CoreTelephony
public struct AppDetail {
static let infoDic = Bundle.main.infoDictionary!;
static let bundleIdentifer = Bundle.main.bundleIdentifier!
static let shortVersion = AppDetail.infoDic[
"CFBundleShortVersionString"]!;
static let buildVersion = AppDetail.infoDic[String(kCFBundleVersionKey)]!;
static let AppName = AppDetail.infoDic[String(kCFBundleNameKey)]!;
}
public struct userDevice {
static let isPhone = UIDevice.current.userInterfaceIdiom == UIUserInterfaceIdiom.phone;
static let isPad = UIDevice.current.userInterfaceIdiom == UIUserInterfaceIdiom.pad;
static let isAppleTV = UIDevice.current.userInterfaceIdiom == UIUserInterfaceIdiom.tv;
static let deviceUUID = UIDevice.current.identifierForVendor!.uuidString;
static let deviceSystemVersion = UIDevice.current.systemVersion;
static let deviceModelName = UIDevice.current.modelName;
static let userPhoneName = UIDevice.current.name;
static let carrierName = CTTelephonyNetworkInfo().subscriberCellularProvider!.carrierName ??
"无服务";
}
public struct deviceScreen {
static let screenWidth = UIScreen.main.bounds.width;
static let screenHeight = UIScreen.main.bounds.height;
}
public extension UIDevice {
var modelName: String {
var systemInfo = utsname()
uname(&systemInfo)
let machineMirror = Mirror(reflecting: systemInfo.machine)
let identifier = machineMirror.children.reduce(
"") { identifier, element
in
guard
let value = element.
value as? Int8,
value !=
0 else {
return identifier }
return identifier + String(UnicodeScalar(UInt8(
value)))
}
switch identifier {
case "iPod5,1":
return "iPod Touch 5"
case "iPod7,1":
return "iPod Touch 6"
case "iPhone3,1",
"iPhone3,2",
"iPhone3,3":
return "iPhone 4"
case "iPhone4,1":
return "iPhone 4s"
case "iPhone5,1",
"iPhone5,2":
return "iPhone 5"
case "iPhone5,3",
"iPhone5,4":
return "iPhone 5c"
case "iPhone6,1",
"iPhone6,2":
return "iPhone 5s"
case "iPhone7,2":
return "iPhone 6"
case "iPhone7,1":
return "iPhone 6 Plus"
case "iPhone8,1":
return "iPhone 6s"
case "iPhone8,2":
return "iPhone 6s Plus"
case "iPhone8,4":
return "iPhone SE"
case "iPhone9,1":
return "iPhone 7"
case "iPhone9,2":
return "iPhone 7 Plus"
case "iPad2,1",
"iPad2,2",
"iPad2,3",
"iPad2,4":
return "iPad 2"
case "iPad3,1",
"iPad3,2",
"iPad3,3":
return "iPad 3"
case "iPad3,4",
"iPad3,5",
"iPad3,6":
return "iPad 4"
case "iPad4,1",
"iPad4,2",
"iPad4,3":
return "iPad Air"
case "iPad5,3",
"iPad5,4":
return "iPad Air 2"
case "iPad2,5",
"iPad2,6",
"iPad2,7":
return "iPad Mini"
case "iPad4,4",
"iPad4,5",
"iPad4,6":
return "iPad Mini 2"
case "iPad4,7",
"iPad4,8",
"iPad4,9":
return "iPad Mini 3"
case "iPad5,1",
"iPad5,2":
return "iPad Mini 4"
case "iPad6,3",
"iPad6,4":
return "iPad Pro 9.7"
case "iPad6,7",
"iPad6,8":
return "iPad Pro 12.9"
case "AppleTV5,3":
return "Apple TV"
case "i386",
"x86_64":
return "Simulator"
default:
return identifier
}
}
}