最新消息:20181230 VPS服务器已从Linode换到腾讯云香港,主题仍用朋友推荐的大前端D8

[已解决]iOS项目编译出错:A declaration cannot be both final and dynamic

iOS crifan 513浏览 0评论

之前项目可以编译和运行,好好的。

去掉了一个不用的库之后,结果再去编译就出错了:

A declaration cannot be both ‘final’ and ‘dynamic’

CompileSwift normal i386 /Users/crifan/dev/dev_root/daryun/Projects/Qoros/QorosSales/Sourcecode/QorosSales/QorosSales/CrifanLib.swift

    cd /Users/crifan/dev/dev_root/daryun/Projects/Qoros/QorosSales/Sourcecode/QorosSales

    /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swift -frontend -c /Users/crifan/dev/dev_root/daryun/Projects/Qoros/QorosSales/Sourcecode/QorosSales/QorosSales/ReportViewController.swift -primary-file /Users/crifan/dev/dev_root/daryun/Projects/Qoros/QorosSales/Sourcecode/QorosSales/QorosSales/CrifanLib.swift /Users/crifan/dev/dev_root/daryun/Projects/Qoros/QorosSales/Sourcecode/QorosSales/QorosSales/TimelineViewController.swift /Users/crifan/dev/dev_root/daryun/Projects/Qoros/QorosSales/Sourcecode/QorosSales/QorosSales/GlobalValues.swift /Users/crifan/dev/dev_root/daryun/Projects/Qoros/QorosSales/Sourcecode/QorosSales/QorosSales/TimelineTableViewCell.swift /Users/crifan/dev/dev_root/daryun/Projects/Qoros/QorosSales/Sourcecode/QorosSales/QorosSales/MainTabViewController.swift /Users/crifan/dev/dev_root/daryun/Projects/Qoros/QorosSales/Sourcecode/QorosSales/QorosSales/Memo.swift /Users/crifan/dev/dev_root/daryun/Projects/Qoros/QorosSales/Sourcecode/QorosSales/QorosSales/GlobalConstants.swift /Users/crifan/dev/dev_root/daryun/Projects/Qoros/QorosSales/Sourcecode/QorosSales/QorosSales/CrifanLibDemo.swift /Users/crifan/dev/dev_root/daryun/Projects/Qoros/QorosSales/Sourcecode/QorosSales/QorosSales/AppDelegate.swift -target i386-apple-ios9.3 -enable-objc-interop -sdk /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator9.3.sdk -I /Users/crifan/Library/Developer/Xcode/DerivedData/QorosSales-fdvmkfwiibblhkeaqjznadykdyuj/Build/Products/Debug-iphonesimulator -F /Users/crifan/Library/Developer/Xcode/DerivedData/QorosSales-fdvmkfwiibblhkeaqjznadykdyuj/Build/Products/Debug-iphonesimulator -F /Users/crifan/dev/dev_root/daryun/Projects/Qoros/QorosSales/Sourcecode/QorosSales/Carthage/Build/iOS -enable-testing -g -import-objc-header /Users/crifan/dev/dev_root/daryun/Projects/Qoros/QorosSales/Sourcecode/QorosSales/QorosSales/QorosSales-Bridging-Header.h -module-cache-path /Users/crifan/Library/Developer/Xcode/DerivedData/ModuleCache -D COCOAPODS -serialize-debugging-options -Xcc -I/Users/crifan/Library/Developer/Xcode/DerivedData/QorosSales-fdvmkfwiibblhkeaqjznadykdyuj/Build/Intermediates/QorosSales.build/Debug-iphonesimulator/QorosSales.build/swift-overrides.hmap -Xcc -iquote -Xcc /Users/crifan/Library/Developer/Xcode/DerivedData/QorosSales-fdvmkfwiibblhkeaqjznadykdyuj/Build/Intermediates/QorosSales.build/Debug-iphonesimulator/QorosSales.build/QorosSales-generated-files.hmap -Xcc -I/Users/crifan/Library/Developer/Xcode/DerivedData/QorosSales-fdvmkfwiibblhkeaqjznadykdyuj/Build/Intermediates/QorosSales.build/Debug-iphonesimulator/QorosSales.build/QorosSales-own-target-headers.hmap -Xcc -I/Users/crifan/Library/Developer/Xcode/DerivedData/QorosSales-fdvmkfwiibblhkeaqjznadykdyuj/Build/Intermediates/QorosSales.build/Debug-iphonesimulator/QorosSales.build/QorosSales-all-non-framework-target-headers.hmap -Xcc -ivfsoverlay -Xcc /Users/crifan/Library/Developer/Xcode/DerivedData/QorosSales-fdvmkfwiibblhkeaqjznadykdyuj/Build/Intermediates/QorosSales.build/all-product-headers.yaml -Xcc -iquote -Xcc /Users/crifan/Library/Developer/Xcode/DerivedData/QorosSales-fdvmkfwiibblhkeaqjznadykdyuj/Build/Intermediates/QorosSales.build/Debug-iphonesimulator/QorosSales.build/QorosSales-project-headers.hmap -Xcc -I/Users/crifan/Library/Developer/Xcode/DerivedData/QorosSales-fdvmkfwiibblhkeaqjznadykdyuj/Build/Products/Debug-iphonesimulator/include -Xcc -I/Users/crifan/Library/Developer/Xcode/DerivedData/QorosSales-fdvmkfwiibblhkeaqjznadykdyuj/Build/Intermediates/QorosSales.build/Debug-iphonesimulator/QorosSales.build/DerivedSources/i386 -Xcc -I/Users/crifan/Library/Developer/Xcode/DerivedData/QorosSales-fdvmkfwiibblhkeaqjznadykdyuj/Build/Intermediates/QorosSales.build/Debug-iphonesimulator/QorosSales.build/DerivedSources -Xcc -DDEBUG=1 -Xcc -DCOCOAPODS=1 -Xcc -working-directory/Users/crifan/dev/dev_root/daryun/Projects/Qoros/QorosSales/Sourcecode/QorosSales -emit-module-doc-path /Users/crifan/Library/Developer/Xcode/DerivedData/QorosSales-fdvmkfwiibblhkeaqjznadykdyuj/Build/Intermediates/QorosSales.build/Debug-iphonesimulator/QorosSales.build/Objects-normal/i386/CrifanLib~partial.swiftdoc -Onone -module-name QorosSales -emit-module-path /Users/crifan/Library/Developer/Xcode/DerivedData/QorosSales-fdvmkfwiibblhkeaqjznadykdyuj/Build/Intermediates/QorosSales.build/Debug-iphonesimulator/QorosSales.build/Objects-normal/i386/CrifanLib~partial.swiftmodule -serialize-diagnostics-path /Users/crifan/Library/Developer/Xcode/DerivedData/QorosSales-fdvmkfwiibblhkeaqjznadykdyuj/Build/Intermediates/QorosSales.build/Debug-iphonesimulator/QorosSales.build/Objects-normal/i386/CrifanLib.dia -emit-dependencies-path /Users/crifan/Library/Developer/Xcode/DerivedData/QorosSales-fdvmkfwiibblhkeaqjznadykdyuj/Build/Intermediates/QorosSales.build/Debug-iphonesimulator/QorosSales.build/Objects-normal/i386/CrifanLib.d -emit-reference-dependencies-path /Users/crifan/Library/Developer/Xcode/DerivedData/QorosSales-fdvmkfwiibblhkeaqjznadykdyuj/Build/Intermediates/QorosSales.build/Debug-iphonesimulator/QorosSales.build/Objects-normal/i386/CrifanLib.swiftdeps -o /Users/crifan/Library/Developer/Xcode/DerivedData/QorosSales-fdvmkfwiibblhkeaqjznadykdyuj/Build/Intermediates/QorosSales.build/Debug-iphonesimulator/QorosSales.build/Objects-normal/i386/CrifanLib.o

<unknown>:0: error: a declaration cannot be both ‘final’ and ‘dynamic’

搜:

A declaration cannot be both ‘final’ and ‘dynamic’

A declaration cannot be both ‘final’ and ‘dynamic’ error in Swift 1.2 – Stack Overflow

现在问题最诡异的是:

为何之前编译没有问题???

并且,对于:

/CrifanLibDemo.swift

代码里面,并没有static之类的

为何也报错?

a declaration cannot be both ‘final’ and ‘dynamic’ – SiamPig的专栏 – 博客频道 – CSDN.NET

[SR-993] Objc class extensions don’t support stored type properties, error message is confusing – Swift

swift/attr_dynamic.swift at master · apple/swift

去看了看final和dynamic:

也都没有我自己写的代码,都是第三方的库

 难道因为是昨天添加了一个:

extension NSDate

{

    static let emptyDate:NSDate = NSDate(timeIntervalSince1970: 0)

}

换种写法试试:

但是此处不是我自己写的NSDate(是系统自己的)

所以没法写成:

class MyClass {

    static var someProperty = 1

}

extension MyClass {

    //something else

}

只能想办法:

 Extensions may not contain stored properties

再去试试其他:

‘let’ declarations cannot be computed properties

最后:

    //static let emptyDate:NSDate = NSDate(timeIntervalSince1970: 0)

    var emptyDate:NSDate {

        return NSDate(timeIntervalSince1970: 0)

    }

至少此处是可以通过编译了

再去clean项目,重新编译,看看能否解决上述的问题

然后之前的调用方式:

if !memoItem.time.isEqualToDate(NSDate.emptyDate) {

也要改为:

if !memoItem.time.isEqualToDate(NSDate().emptyDate) {

最后果然可以正常编译运行项目了。

再去参考:

A declaration cannot be both ‘final’ and ‘dynamic’ error in Swift 1.2 – Stack Overflow

加上:@nonobjc,估计更好:

    //static let emptyDate:NSDate = NSDate(timeIntervalSince1970: 0)

//    var emptyDate:NSDate {

//        return NSDate(timeIntervalSince1970: 0)

//    }

    @nonobjc static let emptyDate:NSDate = NSDate(timeIntervalSince1970: 0)

调用的地方,也又是从:

NSDate().emptyDate

改为之前的:

NSDate.emptyDate

也是可以的。

[总结]

此处的:

extension NSDate

{

    static let emptyDate:NSDate = NSDate(timeIntervalSince1970: 0)

}

报错:

办法1:

如果是自己写的类,可以写成:

class NSDate {

    static let emptyDate:NSDate = NSDate(timeIntervalSince1970: 0)

}

extension NSDate {

    //something else

}

但是很明显,这个NSDate是系统的类,没法这么写。

办法2:

改为:

extension NSDate {

    var emptyDate:NSDate {

        return NSDate(timeIntervalSince1970: 0)

    }

}

-》

当然,对应的调用方式就从:

NSDate.emptyDate

变成了:

NSDate().emptyDate

办法3:

改为:

extension NSDate

{

    @nonobjc static let emptyDate:NSDate = NSDate(timeIntervalSince1970: 0)

}

前提:

是swift 2.0

其他调用此(函数)属性的地方,没有object-c代码

总的来说:

如果条件符合上面,比如我这里就是使用最新的swift 2.2,且调用此段代码的全是swift没有object-c,那么用方法3,比较合适。

转载请注明:在路上 » [已解决]iOS项目编译出错:A declaration cannot be both final and dynamic

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
62 queries in 0.082 seconds, using 9.39MB memory