How do you create a UTType for System Declared Uniform Type Identifiers for folder? - swift

I see all kinds of examples for using a file or an extension but I am trying to call let imag = NSWorkspace.shared.icon(for: <#T##UTType#>) and I don't see any init method in UUType that takes a identifier e.g. folder.

UTType is declared in the Uniform Type Identifiers framework.
All uou need to do is import UniformTypeIdentifiers and then you'll be able to write things like UTType.folder.

Related

Where can I find the official UTTypeItem constant?

It's possible to register an NSView for all dragged types using the elusive UTTypeItem which allegedly replaces kUTTypeItem pasteboard type (now deprecated).
However, when I specify UTTypeItem, the Swift compiler tells me that it can't find this constant.
Eg: controlView.registerForDraggedTypes([UTTypeItem]) gives an error
When I try and compile this code, the Swift compiler helpfully says, "Did you mean kUTTypeItem ?", which gets me nowhere, of course. I tried importing CoreServices but couldn't find it there.
After a bit of digging, I found that I could define UTTypeItem for myself using a rawValue as below:
private let UTTypeItem = NSPasteboard.PasteboardType.init("public.item")
"public.item" is, of course, the under-the-covers value of UTTypeItem. This works fine, and the compiler is happy, but I think this approach is inelegant.
So my question is: where can I fing the "official" location of UTTypeItem ?
Dave
First, to use UTType you need to import UniformTypeIdentifiers:
import UniformTypeIdentifiers
This will then give you access to UTType.item. But since you need a String to pass it to the NSPasteboard, you need to access the identifier property.
let uti = UTType.item.identifier // Gives "public.item"
let pasteboardType = NSPasteboard.PasteboardType.init(uti)
If you need this is more than one place in your project you can add an extension:
import UniformTypeIdentifiers
extension NSPasteboard.PasteboardType {
static let item = NSPasteboard.PasteboardType(UTType.item.identifier)
}
Now you can use it like:
controlView.registerForDraggedTypes([ .item ])

Namespaces between Swift Packages

I am using two swift packages with SPM. But it appears both are defining a struct with the same name. It compiles, though it seems one struct overrides the other. How can I choose which of the structs I want to use? Is there some namespace for each package?
Thank you..
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 everyone manage their firestore paths?

I am using cloud function and client access firestore.but all paths string are hardcode in code. once I changed a collection name I need to change everywhere that the code access same collection. Anyone have a better way to manage them?
Instead of literal strings everywhere, declare a global constant. For example, the usual thing in Swift is a struct or enum with a constant static property.
struct Constants {
static let key = "whatever"
}
Now you can say Constants.key anywhere in your program. Any computer language will have something equivalent.

Access class in different file

I'm still a newbie to swift and I can't get a clear answer on a couple things.
So far I've just been using a single file in playgrounds.
If I want to use more files, how can I access data (variables and functions) from classes created there in my main file that controls the view?
From what I understand having multiple files would just be for convenience so I don't have could to write it again.
(Also on the side) what does it mean when a function has private, public or just 'func'?
I'm using swift 3 playgrounds
Thanks
Making things public will make them importable from other modules. Making it private will make it only accessible by methods within its containing scope (encapsulation). For code that lives at the top level, this scope is the entire .swift file it lives in. Without either access modifier (just bare “func”), your thing will default to internal, which means it is accessible from any other code within the same module, but not by code in a different module.
A special case is the fileprivate modifier which restricts access to the .swift file the code lives in. For code that does not live in a class or struct, this does the exact same thing as private. Some Swift designers discourage use of this modifier, and it may be removed in future versions of Swift.
There is a fifth access modifier in Swift, open, which does the exact same thing as public, except it also allows subclassing, and only applies to classes. This one is rarely used, but useful for certain library interfaces.
To import all the public symbols in a module, use
import Module
To import a single public symbol, use
import var Module.variable
import func Module.function
import struct Module.structure
import class Module.class
...

Can't access constants from one swift file in another

I'm converting some code from Objective C to Swift so I can get a handle on Swift.
Basically, in Objective C, I had a header file containing some global constants which were accessible in other Objective C classes.
I changed my .h file to be .swift and changed the constants to look like:
public let INVALID_INTEGER_VALUE = (-32768)
public let INVALID_LONGLONG_VALUE = (0x8000000000000000)
...
But when I try to access these in another Swift file, the compiler gives "Use of unresolved identifier..." I tried importing my .swift file, but that also didn't work (as far as I can tell, I shouldn't need to import any of my swift files as they are part of the same module).
Any thoughts?
You could also run into this if the file that is trying to use the constant has been added to another target (like the tests target) that the file containing the constants has not been added to.