stringByAppendingPathComponent

stringByAppendingPathComponent

This question already has answers here:
stringByAppendingPathComponent is unavailable
                                
                                    (11个答案)
                                
                        
                                2年前关闭。
            
                    
我刚刚将项目转换为Swift 3,并在directory.path行中遇到了错误:


  错误:类型为'String'的值没有成员'stringByAppendingPathComponent'。


我已经尝试过let realmPath = (directory as NSString).stringByAppendingPathComponent,但出现以下错误:


  无法强制将“ URL”类型的值转换为“ NSString”类型。


override func awake(withContext context: Any?) {
    super.awake(withContext: context)

    let directory: URL = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: "group.RazvanJulian.app")!
    let realmPath = directory.path.stringByAppendingPathComponent("db.realm") /* ERROR */

    RLMRealm.setDefaultRealmPath(realmPath)
    realmToken = RLMRealm.default().addNotificationBlock { note, realm in
        self.reloadTableData()
    }
    reloadTableData()
}


请检查并告知我。

先感谢您!

最佳答案

stringByAppendingPathComponent在Swift 3中成为appendingPathComponent。尝试以下操作:

directory.appendingPathComponent("component")

10-08 08:23