美文网首页
Swift注释 添加编译等

Swift注释 添加编译等

作者: 曹来东 | 来源:发表于2019-08-06 14:30 被阅读0次

MARK TODO FIXME

  • // MARK:类似OC中的#pragma mark
  • ..MARK: -类似OC中的#pragma mark -
  • // TODO:用于标记未完成的任务
  • // FIXME:用于标记待修复的问题
    image.png

条件编译

//操作系统 masOS\iOS\tvOS\watchOS\Linux\Android\Windows\FreeBSD
    #if os(macOS) || os(iOS)
    //CPU架构: i386\x86_64\arm\arm64
    #elseif arch(x86_64) || arch(arm64)
    // swift版本
    #elseif swift(<5) && swift(>=3)
    //模拟器
    #elseif targetEnvironment(simulator)
    //可以导入某模块
    #elseif canImport(Foundation)
    #else
    #endif
  • 可以在项目中添加自定义的宏


    image.png
override func viewDidLoad() {
        
        print("2222")
         #if TEST
         print("test")
         #endif
        print("3333")
        #if OTHER
        print("OTHER")
        #endif
        print("4444")
    }
  • 切换DEBUG Release模式
    image.png
 override func viewDidLoad() {
        
        #if DEBUG
        print("DEBUG")
        #else
        print("Release")
        #endif
    }

打印

func log<T>(_ msg: T,file: NSString = #file,line: Int = #line,fn: String = #function) {
    #if DEBUG
    let prefix = "\(file.lastPathComponent)_\(line)_\(fn):"
    print(prefix,msg)
    #endif
}

系统版本检测

 override func viewDidLoad() {
        if #available(iOS 10, macOS 10.12, *) {
            //对于iOS平台,只在iOS10及以上版本执行
            //对于macOS平台,只在macOS 10.12及以上版本执行
            //最后的*表示在其他所有平台都执行
        }
    }

API可用性说明

//Person在iOS 10, macOS 10.15及以上版本才可用
//* 表示其他平台都可用
@available(iOS 10, macOS 10.15,*)
class Person { }

struct Student {
    //study_ 更名为study
    @available(*,unavailable, renamed: "study")
    func study_() { }
    func study() { }

    //run方法在iOS11 macOS10.12 已弃用
    @available(iOS, deprecated: 11)
    @available(macOS, deprecated: 10.12)
    func run() {
        
    }
}

iOS程序入口

  • AppDelegate上面默认有个@UIApplicationMain标记,表示:编译器自动生成入口代码main函数代码,自动设置AppDelagateAPP的代理

相关文章

网友评论

      本文标题:Swift注释 添加编译等

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