V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
maxCham
V2EX  ›  iOS

Swift 等价于[NSBundle bundleForClass:[self class]]

  •  
  •   maxCham · Jul 11, 2019 · 5293 views
    This topic created in 2482 days ago, the information mentioned may be changed or developed.

    什么是 swift 相当于下一个代码:[NSBundle bundleForClass:[self class]] ?

    目前只知道 Swift = 2.x NSBundle(forClass: self.dynamicType) Swift 3.x Bundle(for: type(of: self))

    但是 Swift4 之后要怎么写呢?请教各位 iOS 大神

    13 replies    2019-07-11 12:30:25 +08:00
    jameskuk
        1
    jameskuk  
       Jul 11, 2019
    你写的 swift3 的 Bundle(for: type(of: self)) 不能再 swift4 上用了吗?
    maxCham
        2
    maxCham  
    OP
       Jul 11, 2019
    不能了
    jameskuk
        3
    jameskuk  
       Jul 11, 2019
    那你试试 Bundle(for: self.classForCoder())
    maxCham
        4
    maxCham  
    OP
       Jul 11, 2019
    我的 type 系一个自定的类型,swift3 时这样写没问题;转 swift4 时就会报这个 type 是 Cannot call value of non-function type
    maxCham
        5
    maxCham  
    OP
       Jul 11, 2019
    Bundle(for: self.classForCoder()) 这个肯定对,因为传进去的就是要 anyClass; 但是既然是 anyClass,为什么我这个自定义的 type 就不行了呢?
    maxCham
        6
    maxCham  
    OP
       Jul 11, 2019
    我的 type 也是这样的一个 class 来的:class var typeList: DisplayType {}
    maxCham
        7
    maxCham  
    OP
       Jul 11, 2019
    public enum DisplayType {
    }
    public final class Display {
    class var typeList: DisplayType {
    }
    }
    maxCham
        8
    maxCham  
    OP
       Jul 11, 2019
    let type = Display. typeList
    let bundle = Bundle(for: type(of: self))
    就会有错
    momocraft
        9
    momocraft  
       Jul 11, 2019   ❤️ 1
    这个帖子第一行是英文机翻出来的吗?
    finab
        10
    finab  
       Jul 11, 2019
    AnyClass 其实是 AnyObject.Type

    如果你要把 typeList 当做 AnyClass
    你需要这样定义
    var typeList: DisplayType.Type
    maxCham
        11
    maxCham  
    OP
       Jul 11, 2019
    哈哈,不是不是,只是搜索到一个帖子的标题,内容都是一样的,所以走来 V 站问问大神,交流交流
    maxCham
        12
    maxCham  
    OP
       Jul 11, 2019
    谢谢大神们,我找到了,原来不是语法问题;是我理解错了,用会 Bundle(for: self.classForCoder())就可以了
    onevcat
        13
    onevcat  
       Jul 11, 2019
    大概这样就行了?

    Bundle(for: Self.self)
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   6166 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 58ms · UTC 02:16 · PVG 10:16 · LAX 19:16 · JFK 22:16
    ♥ Do have faith in what you're doing.