XCode 9 issue. Failed to set user defined property in interface builder - ios

My issue is that settings a keypath and value in interface builder does not seem to be working in XCode 9. There is a work around where you make the variable inspectable.
I've created a sample project and details below.
I created a simple test class where I set properties given a path and a value, like so.
You'll see there are two strings being set, ignore backgroundColor that was a sanity check, key path "string" works but key path "string2" does not. "string" And "string2" differ in one way and that's in their declaration as shown here.
#IBInspectable var string: String = ""
var string2: String = ""
The result of all this is code is shown here.
I used to do this all the time in XCode 8, ios9 without issue, did I miss something with the update? I have a work around but the issue still vexes me.

// mark the line as #objc
#objc var string: String
This was the answer given by dan in the comments.

Related

How to use dynamic localization keys with SwiftUI

I'm trying to display a localized text based on an ID. I have a lot of these, so it feels more efficient to just add dynamically the ID to the string.
The string file looks like this:
"users.1.name" = "Alice"
"users.2.name" = "Bob"
"users.3.name" = "Charles"
...
If I do the following, hardcoding the ID, it works as expected and displays the associated translated key:
Text("users.1.name")
However if I do this it only displays the string:
Text("users.\(user.id).name")
// displays "users.1.name" instead of "Alice"
I've also tried:
Text(LocalizedStringKey("users.\(user.id).name"))
// displays "users.1.name" instead of "Alice"
Am I missing something or is this not possible?
You need then the following. Tested with Xcode 11.4 / iOS 13.4
Text(NSLocalizedString("users.\(id).name", comment: ""))
I'm not a SwiftUI expert but you can use:
let key:String = "users.\(user.id).name"
Text(LocalizedStringKey(key))
// OR compact
Text(LocalizedStringKey(String("users.\(user.id).name")))

How to dynamically set binding type's "formatOptions" and "constraints" in XML with binding?

I have a list of elements (OData set) and use a binding to show this list.
One field is for a quantity value and this value could sometimes need some decimal places.
The requirement is: only show that amount of decimal numbers that is also available in the OData service.
Annotation techniques can't be used.
I 'hacked' something that is misusing a formatter to update the type of a binding. But this is 'a hack' and it is not possible to convert it to XML views. (The reason is a different handling of the scope the formatter will be called).
So I am searching for a working solution for XML views.
The following code would not work but shows the issue:
new sap.m.Input({ // looking for an XML solution with bindings
value: {
path: "Quantity",
type: new sap.ui.model.type.Float({
// formatOptions
maxFractionDigits: "{QuantityDecimals}",
// ...
}, {
// constraints
minimum: 0
}),
// ...
}
});
The maxFractionDigits : "{QuantityDecimals}" should be "dynamic" and not a constant value.
Setting formatOptions and constraints dynamically in XML (via bindings or a declared function) is unfortunately not (yet) supported. But IMHO this is a valid enhancement request that app developers would greatly benefit from, since it encourages declarative programming.
I already asked for the same feature some years ago but in a comment at https://github.com/SAP/openui5/issues/2449#issuecomment-474304965 (See my answer to Frank's question "If XMLViews would allow a way to specify the dynamic constraints as well (e.g. enhanced syntax), would that fix the problem?").
Please create a new issue via https://github.com/SAP/openui5/issues/new with a clear description of what kind of problems the feature would resolve and possibly other use cases (You can add a link to my comment). I'll add my 👍 to your GitHub issue, and hopefully others too.
I'll update this answer as soon as the feature is available.
Get your dynamic number from your model and store it in a JS variable.
var nQuantityDecimals = this.getModel().getProperty("/QuantityDecimals");
new sap.m.Input({
value : {
path : "Quantity",
type : new sap.ui.model.type.Float({
maxFractionDigits : nQuantityDecimals,
source : {
groupingSeparator: ",",
decimalSeparator: ".",
groupingEnabled: false
}
}, {
minimum:0
})
}
}),

Swift 3 : List of files Optional()

i'm going nuts.
I'm trying to get the list of "txt" files from a folder but i get the list in the form of
Optional("Filename").txt
Here's my code, nothing fancy. I tried unwrapping the filename but the compiler gives me an error.I tried a "guard", a with "if", i tried "!" ... nothing works.
let enumerator:FileManager.DirectoryEnumerator = FileManager().enumerator(atPath: myFolderPath)!
while let element = enumerator.nextObject() as? String {
print(element)
if element.hasSuffix("txt") {
fileList.append(element)
}
}
I need to show this list in a table view.
I hope i'm not supposed to run trought the array and get the name of the files by using a bunch o string methods just to get rid of this text...
I'm not sure what can i do! I don't really want to use this solution:
Swift: Optional Text In Optional Value
Thank you
# Vadian and Rob: you guys rock, you asked the right question.
The issue was that i'm naming the .txt files with a textfield and i'm doing all the necessary work to make sure that this textfield is not empty or otherwise.
I never thought that this textfield could be optional, hence the Optional("Filename").txt
Once i unwrapped the value from the textfield Optional() had gone.
Thank you.

action script 2.0 simple string comparing with conditional statement

So I have this crazy problem with comparing 2 strings in ActionScript 2.0
I have a global variable which holds some text (usually it is "statistic") from xml feed
_root.var_name = fields.firstChild.attributes.value;
when I trace() it it gives me the expected message
trace(_root.var_name); // echoes "statistik"
and when I try to use it in conditional statement the rest of code is not being executed because comparing :
if(_root.overskrift == "statistik"){
//do stuff
}
returns false!
I tried also with:
if(_root.overskrift.equals("statistik"))
but with the same result.
Any input will be appreciated.
Years later but on a legacy project I had the same problem and the solution was in a comment to this unanswered question. So let's make it an anwer:
Where var a:String="some harmless but in my case long string" and var b:String="some harmless but in my case long string" but (a==b) -> false the following works just fine: (a.indexOf(b)>=0) -> true. If the String were not found indexOf would give -1. Why the actual string comparison fails I coudn't figure out either. AS2 is ancient and almost obsolete...

How to internationalize the UITextField placeholder property value string in interface builder itself?

How can we internationalize the placeholder property's value of a UITextField in interface builder-attribute inspector?
You can easily change it without writing any code.
here is the example in your localized strings file set your placeholder as follows
"itw-s8-fkt.placeholder" = "YOUR TEXT";
You do the same localization as usual:
myTextField.placeholder = NSLocalizedString(#"emptyUsername",
#"userNameTextFieldPlaceholder");
Then you enter the according string in your localizable strings file.
The question has been discussed here, too:
How to use NSLocalizedString in IB [iPhone SDK]?
Describing an alternative, using several xib files.
What I've done, to minimise the amount of localisation boilerplate, is follow the model outlined here (GitHub here): create Interface Builder-friendly helper extensions (#IBInspectable vars, in a single file for convenience) for all the types of controls I want localised, including one for UITextField placeholders. I can then centralise all localisations to a single .strings file for each language and hook them up via IB. The only downside is remembering to add the keys when I create the controls. Localisation debugging (set by editing the Run scheme; I run the app in a non-base language by default) helps spot these early.
I have a single Excel spreadsheet containing keys and translations that gets passed to local translators to update, and a script that (re)generates the .strings files from the .csv prior to release.
With reference to the linked tutorial, my UITextField extension looks like this:
extension UITextField: XIBLocalizable {
#IBInspectable var xibLocKey: String? {
get { return nil }
set(key) {
text = key?.localized
}
}
#IBInspectable var xibPlaceholder: String? {
get { return nil }
set(key) {
placeholder = key?.localized
}
}
}
(The linked GitHub has an almost identical version I've only just spotted). Worth noting that when adding a new localisation I have to manually delete the contents of the XCode autogenerated .strings file, while leaving the file in place, to ensure the extensions get called.

Resources