This question already has an answer here:
How do I lookup a string constant at runtime in Objective-C?
(1 answer)
Closed 8 years ago.
Is it possible to dynamically reference a constant in objective c:
e.g. NSForegroundColorAttributeName is declared in UIKit/NSAttributedString.h as a const NSString. Its value is #"NSColor". In compiled code, I just specify the constant to use it, but what if I need to refer to it dynamically at run time (say a user might type it out)?
I could just use #"NSColor", but that may change as the SDK changes.
Is there a function like NSClassFromString, say ConstFromString:
ConstFromString(#"NSForegroundColorAttributeName") --> #"NSColor"
If the symbol is a public, exported symbol, then you might be able to look it up at runtime using dlsym and friends, but I agree with the commenter that this sounds like a bad idea.
Related
This question already has answers here:
Properties vs Methods
(16 answers)
Closed 3 years ago.
When is it preferable to use getters instead of functions in Dart?
In other words, when would you use getters to get something, which at the same time could be also got with a function?
You can read from the effective dart guide:
DO use getters for operations that conceptually access properties.
The operation does not take any arguments and returns a result.
The caller cares mostly about the result.
The operation does not have user-visible side effects.
The operation is idempotent.
The resulting object doesn’t expose all of the original object’s state.
This question already has an answer here:
What are the semantics of _internal?
(1 answer)
Closed 4 years ago.
I have the following code in a flutter tutorial that discusses global state:
static GlobalState instance = new GlobalState._();
GlobalState._();
What exactly is the ._ doing? Is this technically just a blank method?
It's a private constructor. This pattern is used to prevent clients from instantiating or subclassing GlobalState, so the only available instance of GlobalState in the program is instance.
This question already has answers here:
What are function typedefs / function-type aliases in Dart?
(7 answers)
Closed 4 years ago.
This seems like a very obvious question to me, but I cannot seem to find the answer.
How do I write a function type in Dart.
I can use some canned forms like ValueChanged<T> or ValueSetter<T>, but how are they defined? How can I write general function types like T -> U -> V
If I understood your question correctly, I think you are referring to Generic Types. These types are used by functions when you are not sure which type your function is going to use in order to avoid code duplication. For example, let's say you have a class with a method that stores numbers in a List:
class StoreInt {
storeInList(int value);
}
But later, you decide you now want it to store Strings, so you create another class:
class StoreString {
storeInList(String value);
}
Generics allow you to write this class once and use it to store any kind of object you want:
class Store<T> {
storeInList(T value);
}
As far as I know, you don't create generics in dart, you just specify a single letter in between diamond operators as shown in the previous example.
You can find more information on generics here: https://www.dartlang.org/guides/language/language-tour#generics
This question already has answers here:
How to initialize properties that depend on each other
(4 answers)
Instance member cannot be used on type of custom class
(3 answers)
Closed 6 years ago.
I'm currently working on learning Swift 3 and I'm trying to implement an XML parser that parses data from a URL. I've created a separate file to handle the XML parsing and have setup it as follows.
let xmlDoc = NSURL(string: "http://labs.bible.org/api/?passage=random&type=xml")
var parser1 = XMLParser(contentsOf: xmlDoc)
I'm currently having issues with syntax however. Swift is highlighting the line where I initialize the XML Parser and giving the error "Cannot use instance member 'xmlDoc' within property initializer, property initializers run before 'self' is available.
This is probably something really simple but I wasn't able to find a solution in my google searches.
Any feedback is appreciated!
-Alex
I'm not sure if this is the right place to ask this, since it's not really a technical question but more a question of style and coding practices...
I've always ben a fan of using "const" to define variables that will not be changing throughout their lifetime, most especially when they are parameters to functions/methods. This probably stems from my history with C++, where objects could be passed by reference rather than by pointer, but you wanted to ensure that the original value wasn't accidentally altered, either by you or by someone else on your team who was working on the same code snippet.
When looking through the headers for both Objective-C in general and Cocos2d specifically, I've noticed that there is a noticeable lack of use of this item. Now, I'm not against developing code as quickly as possible, and leaving off constraints such as these leave the developer the option to modify values as their code develops and evolves, but there are some instances where I believe that this laxity does not belong.
For example, in Cocos2D/UIKit, the "UIFont fontWithName" method takes "(NSString *)" as the parameter for the font name: does this method really need to reserve the right to alter the original string that was passed in? I personally like to define constant strings as "const" items, and I don't like the necessity of casting these as non-"const" when calling these methods.
Enough proselytizing: My question - Is the direction now moving towards less well-defined interfaces and more towards "lazy references" (which I do not consider to be a derogative term)?
Thanks in advance for any feedback....
Const wouldn't mean anything for Objective C class pointers, because it would have to be overloaded in a very confusing way for Objective C types. This is because there's no way to mark a method as const, as there is in C++, so the compiler could never enforce it.
That said, at my old company, we did declare global string constants using something like:
NSString* const kMyCoolString = #"Hello, world!";
The point being that it at least couldn't be reassigned to something else.
The closest analog in Objective C/Cocoa/Foundation are mutable/immutable versions of data structures, which doesn't really help your case.