How to use Namespaces in Swift?
Even though it is possible to implement namespaces using Framework and Libraries but the best solution is to use local packages using Swift Package Manager. Besides having access modifiers, this approach has some other benefits. As in Swift Package Manager, the files are managed based on the directory system, not their target member ship, you won't have to struggle with merge conflicts that arise frequently in teamworks. Furthermore, there is no need to set file memberships.
To check how to use local Swift packages refer to the following link:
Organizing Your Code with Local Packages
Namespaces and modules in the Swift language
Turns out that this is a known bug: https://devforums.apple.com/message/976286#976286
Namespaces between Swift Packages
Let say you have package ‘A’ and ‘B’ and the struct name is ‘SomeModel’
You can simply
import A
import B
let modelA = A.SomeModel(…)
let modelB = B.SomeModel(…)
This is the default behavior/namespacing for different modules/packages in Swift. Though there might be an additional encapsulating/name-spacing within the package. Something like
class SomeClass {
struct SomeModel{}
}
Then you can access it with the additional encapsulation
How to use Namespace in SocketIOClient in swift
This should be enough :
class SocketIOManager: NSObject {
static let sharedInstance = SocketIOManager()
let manager = SocketManager(socketURL: URL(string: "")!, config: [.log(false), .compress, .forcePolling(false)])
var avaialableCallBack:(([Any]) -> Void)?
override init(){
super.init()
}
func establishConnection() {
let socket = manager.socket(forNamespace: "/consumer")
socket.on("connect") { (data, ack) -> Void in
print("socket connected",data,ack)
}
socket.on(clientEvent: .disconnect){data, ack in
print("socket disconnected")
}
socket.on("session-available") { (dataArr, ack) -> Void in
ack.with(true)
if let sessionAvailableCB = self.avaialableCallBack {
sessionAvailableCB(dataArr)
}
}
socket.connect()
}
func closeConnection() {
let socket = manager.socket(forNamespace: "/consumer")
socket.disconnect()
}
func emitMessage(message:String,data:[String:Any]){
let socket = manager.socket(forNamespace: "/consumer")
socket.emit(message,data)
}
func emitMessageWithAck(message:String,data:[String:Any]) -> OnAckCallback{
let socket = manager.socket(forNamespace: "/consumer")
return socket.emitWithAck(message, data)
}
}
How to change the namespace of a Swift class?
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
.
This is always a concern when you're translating from Objective-C to Swift and you've got an existing archive to deal with. See Apple's documentation:
https://developer.apple.com/library/ios/documentation/Swift/Conceptual/BuildingCocoaApps/InteractingWithObjective-CAPIs.html
Note the discussion under "Exposing Swift Interfaces in Objective-C".
Namespace in Swift: Differentiate between an enum and a class
It turns out you can make a file that has a typealias
import class Fruits.Banana
typealias BananaClass = Fruits.Banana
Then just use BananaClass
instead in ClassA
Related Topics
Difference Between Dispatch_Async and Dispatch_Sync on Serial Queue
Resizing a Uilabel to Accommodate Insets
Save Images with Phimagemanager to Custom Album
How to Change Wkwebview or Uiwebview Default Font
Alamofire Invalid Value Around Character 0
Perform Background Tasks When App Is Terminated
How to Determine Height of Uicollectionview with Flowlayout
Sharing Data Between an iOS 8 Share Extension and Main App
Wkwebview Persistent Storage of Cookies
How to Add Animation to Launch Screen in iOS 9.3 Using Objective C
How to Update a Localized Storyboard's Strings
Multiple Uilabels Inside a Self Sizing Uitableviewcell
How to Put an Image in a Realm Database
How to Load an Uiimage into a Swiftui Image Asynchronously
Xcode Canvas for Swiftui Previews Does Not Show Up