欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Swift 中的结构体 (Struct)

程序员文章站 2024-03-23 12:23:34
...

结构体的介绍

  • 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
    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
    }
    //构造方法2
    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
    }
}

创建&&输出
Swift 中的结构体 (Struct)

下面是一个通过定义几个 Struct 获取 App 信息 、设备信息的工具类。

import Foundation
import UIKit
import CoreTelephony //获取运行商需要导入

//App 相关信息
public struct AppDetail {

    //info,plist
    static let infoDic = Bundle.main.infoDictionary!;

    //bundle identifer 
    static let bundleIdentifer = Bundle.main.bundleIdentifier!

    //version版本号 
    static let shortVersion = AppDetail.infoDic["CFBundleShortVersionString"]!;

    //build版本号
    static let buildVersion = AppDetail.infoDic[String(kCFBundleVersionKey)]!;

    //app名称
    static let AppName = AppDetail.infoDic[String(kCFBundleNameKey)]!;


}

//用户设备信息
public struct userDevice {

    //是否是IPhone
    static let isPhone = UIDevice.current.userInterfaceIdiom == UIUserInterfaceIdiom.phone;
    //是否是isPad
    static let isPad = UIDevice.current.userInterfaceIdiom == UIUserInterfaceIdiom.pad;
    //是否是isPad
    static let isAppleTV = UIDevice.current.userInterfaceIdiom == UIUserInterfaceIdiom.tv;

    //备唯一标识符 UUID
    static let deviceUUID = UIDevice.current.identifierForVendor!.uuidString;

    //设备的系统版本
    static let deviceSystemVersion = UIDevice.current.systemVersion;

    //获取设备具体型号:(iphone 6s plus)
    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;


}

//MARK: - UIDevice延展
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
        }
    }
}