问题
I'm attempting to rebuild Apple's XPC "lowerCase" sample code from Objective-C to Swift. I understand XPC will, but I'm relatively new to Swift and Objective-C interoperability.
When I use their exact sample code, which passes a String
from the app to the target and back, it works. But when I try and replace that with my own custom class, Person
, I get the error:
NSXPCConnection: ... connection on anonymousListener or serviceListener from pid 4133:
Exception caught during decoding of received selector upperCasePerson:withReply:, dropping incoming message.
Exception: Exception while decoding argument 0 (#2 of invocation):
Exception: decodeObjectForKey: class "CombineCycle.Person" not loaded or does not exist.
(The demo apps name is 'CombineCycle')
Person
is a Swift class that inherits from NSObject
and conforms to NSSecureCoding
. It has a single property for name:String
. It's accompanying .swift
source file is included in the XPC target's build.
The exception seems pretty self-explanatory but I'm not sure which build settings I need to change, or which files I need to generate, to fix the underlying issue.
This is just a demo app. The main app was created as a Swift app and contains no Objective-C files. The XPC target was created using Xcode's new target feature which, in Xcode 11, generates an Objective-C skeleton. I replaced all of those files with Swift implementations.
// Person.swift (Included in both targets)
@objc public class Person : NSObject, NSSecureCoding {
var name:String?
init(_ name:String) {
self.name = name
}
public static var supportsSecureCoding: Bool {
return true
}
public required init?(coder: NSCoder) {
self.name = coder.decodeObject(forKey: "name") as? String
}
public func encode(with coder: NSCoder) {
coder.encode(self.name, forKey: "name")
}
}
Update
The XPC service is able to instantiate an instance of Person
just fine, so the class is being included. The problem appears to be that NSXPCDecoder
(a private class) is unable to deserialize it...
__exceptionPreprocess + 250
objc_exception_throw + 48
_decodeObject + 1413
-[NSXPCDecoder _decodeObjectOfClasses:atObject:] + 63
_NSXPCSerializationDecodeInvocationObjectOnlyArgumentArray + 463
回答1:
The problems appears to be related to how NSCoder does archiving and unarchiving by relying on the classe's name. When the Person
object is encoded on the application side, the name used is <ModuleName.ClassName>
.
When attempting to deserialize that in the service, ModuleName is naturally wrong. Using @objc(Person)
on the Person to set the Objective-C names appears to be working.
来源:https://stackoverflow.com/questions/56585775/how-do-you-make-a-swift-class-available-in-an-xpc-service