简体   繁体   English

如何更改Swift类的命名空间?

[英]How to change the namespace of a Swift class?

When you implement a class MyGreatClass in Swift its fully qualified name will by <MyPackageName>.MyGreatClass . Swift中实现MyGreatClassMyGreatClass ,其完全限定名称将由<MyPackageName>.MyGreatClass This is different to Objective-C , where the fully qualified name of that same class is MyGreatClass . 这与Objective-C不同,其中同一类的完全限定名称是MyGreatClass

Unfortunately this introduces a problem for me. 不幸的是,这给我带来了一个问题。 When I am using NSUnarchiver and the archive was written with Objective-C objects I cannot unpack it with Swift -classes(see below for a detailed description). 当我使用NSUnarchiver并且使用Objective-C对象编写存档时,我无法使用Swift -classes解压缩它(请参阅下面的详细说明)。

This means I need to find a way to rename the namespace for my Swift classes. 这意味着我需要找到一种方法来重命名我的Swift类的命名空间。 How do I do that? 我怎么做?

Any help would be great! 任何帮助都会很棒!

Background: Why can't NSUnarchiver see/load my swift class? 背景:为什么NSUnarchiver看不到/加载我的swift类?

I have implemented a small program to read a file, which was archived with NSArchive . 我已经实现了一个小程序来读取一个文件,该文件是使用NSArchive存档的。
These are my files: 这些是我的文件:

main.swift : main.swift

import Foundation

// parse command line - total path to *.trace file (from Apple's Instruments app)
var traceFilePath = Process.arguments[1]

var traceFile = NSURL(fileURLWithPath: traceFilePath)
var error:NSError?

// check if the file exists
if (traceFile?.checkResourceIsReachableAndReturnError(&error) == false){
    // file does not exist or cannot be accessed
    println("\(error)")
    exit(1)
}

var rawData = NSData(contentsOfURL: traceFile!)
var data = NSUnarchiver(forReadingWithData: rawData!)
var decodedObject: AnyObject? = data?.decodeObject()

XRObjectAllocRun.swift : XRObjectAllocRun.swift

import Foundation

class XRObjectAllocRun: NSObject {
    // class to be implemented   
}

When I now run my application on an Instruments -file I am getting the following error: Terminating app due to uncaught exception 'NSArchiverArchiveInconsistency', reason: '*** class error for 'XRObjectAllocRun': class not loaded' . 当我现在在Instruments -file上运行我的应用程序时,我收到以下错误: Terminating app due to uncaught exception 'NSArchiverArchiveInconsistency', reason: '*** class error for 'XRObjectAllocRun': class not loaded'

This is really strange because when I add the exact same class in an Objective-C file with a bridging header file I have no issues. 这真的很奇怪,因为当我在带有桥接头文件的Objective-C文件中添加完全相同的类时,我没有任何问题。

trace file reader-Bridging-Header.h : is empty. trace file reader-Bridging-Header.h :为空。

XRObjectAllocRun.h : XRObjectAllocRun.h

#import <Foundation/Foundation.h>
@interface XRObjectAllocRun : NSObject
@end

XRObjectAllocRun.m : XRObjectAllocRun.m

#import "XRObjectAllocRun.h"
@implementation XRObjectAllocRun
@end

What am I missing? 我错过了什么? Why is my Objective-C class found, whereas my Swift class is not? 为什么找到我的Objective-C类,而我的Swift类不是? Swift has no issues for example with var x = XRObjectAllocRun() in main.swift , but yet the NSUnarchiver still complaints about a missing XRObjectAllocRun class when I stay purely within Swift . Swift在main.swift没有问题,例如var x = XRObjectAllocRun() ,但是当我纯粹在Swift时, NSUnarchiver仍然抱怨缺少XRObjectAllocRun类。 Is the NSUnarchiver looking in the wrong places - does it for some reason only accept Objective-C classes? NSUnarchiver是否在寻找错误的位置 - 由于某种原因它是否只接受Objective-C类?

If you want to know what I am trying to do check this stackoverflow question out. 如果你想知道我想要做什么,请检查这个stackoverflow问题

Update 更新

This is what apple writes: 这就是苹果写的:

Swift classes are namespaced based on the module they are compiled in, even when used from Objective-C code. Swift类基于它们编译的模块进行命名空间化,即使在使用Objective-C代码时也是如此。 Unlike Objective-C, where all classes are part of a global namespace 与Objective-C不同,其中所有类都是全局命名空间的一部分

Further more: 更多:

For example, when you create a document–based Mac app, you provide the name of your NSDocument subclass in your app's Info.plist file. 例如,在创建基于文档的Mac应用程序时,可以在应用程序的Info.plist文件中提供NSDocument子类的名称。 In Swift, you must use the full name of your document subclass, including the module name derived from the name of your app or framework. 在Swift中,您必须使用文档子类的全名,包括从应用程序或框架名称派生的模块名称。

Yikes, trying to figure out the mess now... 哎呀,试图弄清楚现在的混乱......

Try this when you declare your class: 声明你的课时试试这个:

@objc(XRObjectAllocRun) class XRObjectAllocRun: NSObject {
    // class to be implemented   
}

That will give this class the same name as the archived class, namely XRObjectAllocRun , instead of the namespaced Swift name trace_file_reader.XRObjectAllocRun . 这将使该类与归档类具有相同的名称,即XRObjectAllocRun ,而不是命名空间的Swift名称trace_file_reader.XRObjectAllocRun

This is always a concern when you're translating from Objective-C to Swift and you've got an existing archive to deal with. 当您从Objective-C转换为Swift并且您已经有一个现有的存档要处理时,这始终是一个问题。 See Apple's documentation: 请参阅Apple的文档:

https://developer.apple.com/library/ios/documentation/Swift/Conceptual/BuildingCocoaApps/InteractingWithObjective-CAPIs.html https://developer.apple.com/library/ios/documentation/Swift/Conceptual/BuildingCocoaApps/InteractingWithObjective-CAPIs.html

Note the discussion under "Exposing Swift Interfaces in Objective-C". 请注意“在Objective-C中公开Swift接口”下的讨论。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM