Xcode 10 code completion placeholders — what is the "T"? - ios

In Xcode 10 code completion, the text underlying the placeholder tokens has an extra #T# before it (to see that this is so, copy and paste the inserted code template into a different text editor):
let alert = UIAlertController(
title: <#T##String?#>, message: <#T##String?#>,
preferredStyle: <#T##UIAlertController.Style#>)
What is that? Does "T" mean Type? What difference does it make in my usage of the placeholder?

The syntax <#T##_A_##_B_#> is used to specify a default piece of code for the placeholder. The placeholder will be displayed as _A_, but when the user presses the enter key the placeholder will be replaced with _B_.
Placeholder:
After pressing enter:
It'd a useful feature when presenting something to an audience, because as opposed to snippets, I would't need to remember the name of each snippet, I'd just select a placeholder and press enter to get the right piece of code.
EDIT:
Answering your question, indeed it seems that the T refers to type. If you try to replace the placeholder with an expression, like <#T##Example1##let i = 3#>, the placeholder it's not replaced with let i = 3 as you would expect. It is instead replaced with <<error type>>.
Furthermore, this placeholder <#T##transform: (Error) throws -> U?##(Error) throws -> U?#> is replaced with:
{ (<#Error#>) -> U? in
<#code#>
}
My guess is that when you prepend the T you are telling Xcode that you will provide a type, then Xcode finds an appropriate default value for that type.

Related

How change app language in real time?

I am working on an app which runs in 2 languages, English and Persian (farsi). So user can select his desired language and app is displayed in that language. What should I do?
Depends on what are your trying to achieve, there is an approach that might be useful to your case, which is:
Declare a global variable which should represents the key of the current used language in the app, call it appLanguageKey String -for example-:
var appLanguageKey = "english"
Create a datastore for storing both languages desired caption for each term, it might looks like (contains):
term_key term_english term_farsi
greeting_key Hello Hoi
bye_key Bye Doei
For now, you could get the desired value if you tried to do:
desiredTerm = "select term_\(appLanguageKey) where term_key = 'greeting_key'"
Consider it as pseudo-code, the condition should be similar to it.
By implementing such a function that returns a string (I will assume that it is: getDesiredCaption(_ termKey: String) -> String in the following code snippet), you will be able to automatically set the desired caption for any UI component, just call it in viewWillAppear method the view controller:
override func viewWillAppear(_ animated: Bool) {
.
.
.
label.text = getDesiredCaption("greeting_key")
// Since that 'appLanguageKey' global viriable is "english",
// the output should be "Hello"
.
.
.
}
All you have to do for changing the language of the app is to change the value of appLanguageKey to "farsi".
For another approach which related to the Internationalization and Localization, you might want to check this answer, it should affect the app to let its navigation to be from right to left.
Hope this helped.
First you should use realm, magical records or an API to store what the user picks. Then you could create your own type of localizeable string handeler, by having an array with 2 dictionaries 1 with english and 1 with persian that all the texts in your application will read from just as you do with a normal localizeable.

Can't get access to string localizations in UI Test (Xcode 7)

So I have a situation in which I have a few textFields that are validated. I'm trying to run a UI test, and when they fail they will get an alert to pop up with an error message (potentially a different message depending on what fields are invalid and in what way).
I'd like to test that not only that an alert appeared, but that the correct message is displayed. The problem I'm having is that I need to get the localized text for comparison (if I run tests in another language other than english), but when I call NSLocalizedString in the UITest it can't gather the correct localized string (just returns the key [default])
I've tried adding the localizeable.strings files to the UITest target, but to no avail. Does anyone know if this is possible?
edit as a side note: I also tried setting an accessibility identifier on the UIAlertView but when I query with that accessibility identifier it doesn't exist, I can only query it using the title of the alert which seems backwards.
In the UI tests, the main bundle seems to be a random launcher app. That's why the .strings file doesn't appear: even though you added it to your test bundle, NSLocalizedString is checking the wrong bundle. To get around this, you need an invocation like this:
NSLocalizedString("LOCALIZATION_KEY", bundle: NSBundle(forClass: AClassInYourUITests.self), comment: "")
Which you might want to pull into a helper method.
Here is my solution:
In your UI Tests target -> Build Phases -> Copy Bundle Resources, add the needed localization files (e.g. Localizable.strings).
Add a function similar to the following:
func localizedString(key:String) -> String {
/*1*/ let localizationBundle = NSBundle(path: NSBundle(forClass: /*2 UITestsClass*/.self).pathForResource(deviceLanguage, ofType: "lproj")!)
/*3*/ let result = NSLocalizedString(key, bundle:localizationBundle!, comment: "") //
return result
}
/*1 Gets correct bundle for the localization file, see here: http://stackoverflow.com/questions/33086266/cant-get-access-to-string-localizations-in-ui-test-xcode-7 */
/*2 Replace this with a class from your UI Tests*/
/*3 Gets the localized string from the bundle */
Use the function like this: app.buttons[localizedString("localized.string.key")]
I have another approach written here: https://stackoverflow.com/a/75178860/7715250. Copy paste for when the question is deleted/closed:
Different approach without calling methods on your String
Prerequisites
You are using NSLocalizedString.
Step 1
Make sure you add the translations to your test targets (Go to your
Localizable file and on the right side you can tap your UI test
targets).
Step 2
Add this somewhere in your main target
#if DEBUG
// Can be changed by UI tests to get access to localized content
var bundleForLocalizedTexts = Bundle.main
#else
let bundleForLocalizedTexts = Bundle.main
#endif
Step 3
Add this value to the parameter bundle in all your
NSLocalizedStrings, like this:
NSLocalizedString(
"localized",
bundle: bundleForLocalizedTexts,
comment: ""
)
Step 4
Override the method setUp in your XCTestCase subclass and add this
line:
bundleForLocalizedTexts = Bundle(for: MySubclass.self)
Step 5
Everything should work! All languages should work, no extra methods to call.

Check if entered text is valid in Xtext

lets say we have some grammar like this.
Model:
greeting+=Greeting*;
Greeting:
'Hello' name=ID '!';
I would like to check whether the text written text in name is a valid text.
All the valid words are saved in an array.
Also the array should be filled with words from a given file.
So is it possible to check this at runtime and maybe also use this words as suggestions.
Thanks
For this purpose you can use a validator.
A simple video tutorial about it can be found here
In your case the function in the validator could look like this:
public static val INVALID_NAME = "greeting_InvalidName"
#Check
def nameIsValid(Greeting grt) {
val name = grt.getName() //or just grt.Name
val validNames = NewArrayList
//add all valid names to this list
if (!validNames.contains(name)) {
val errorMsg = "Name is not valid"
error(errorMsg, GreetingsPackage.eINSTANCE.Greeting_name, INVALID_NAME)
}
}
You might have to replace the "GreetingsPackage" if your DSL isn't named Greetings.
The static String passed to the error-method serves for identification of the error. This gets important when you want to implement Quickfixes which is the second thing you have asked for as they provide the possibility to give the programmer a few ideas how to actually fix this particular problem.
Because I don't have any experience with implementing quickfixes myself I can just give you this as a reference.

Long strings in system Keyboard plist settings

I try add my own characters in keyboard.
I expand key 'z' by code to "Keyboard-en.plist":
"Roman-Accent-z" = {
Keycaps = "z mylongstringtitle ..."; // ... == \U017e \U017a \U017c characters ('z' with apostrophes and dots)
Strings = "z mylongstringvalue ...";
};
But keyboard not shown my string 'mylongstringtitle', only blank space ' ':
I can add only string 4 chars length on normal font and 7 chars on small font on 'button'. But I need add a long string.
Can I add 'mylongstringtitle' inside keycaps's string? May be I need set special parameters for long string? This is possible?
I don't think you will be able to add long string to be displayed in keyboard-en.plist. I believe the limitations of 4/7chars that you discovered are hard-coded in desire to avoid disfigured popup.
This keyboard behavior is not supported by Apple (it's not documented on http://developer.apple.com, or http://devforums.apple.com). Use of this feature in your app, therefore, is undefined behavior, which is considered programmer error.
That said, it's a neat feature. The limits of 4 and 7 characters for keycaps are hardcoded on iOS. I can't find them in any of the keyboard plists. You'd probably have to modify the keyboard binary to do what you want. (On OS X, on the other hand, this limit does not exist.)
A few alternatives exist, which you might know about:
You could modify this custom keyboard project
You could assign a custom inputView to be displayed at the top of the system keyboard

Issue with using int.parse() in Dart

I'm currently teaching myself the Dart language, and my first app doesn't seem to be working right. Here's the code that's causing trouble:
usrLoc = int.parse(query("#txtLoc").text);
When I try to run the app, it opens fine, but when I click the button that triggers this (and three other similar parses), the debugger stops and tells me "Source not found" for int._native_parse(), int._parse(), and int.parse().
Any help would be greatly appreciated.
The text property for the specified element #txtLoc returns an empty string.
The parse method requires that:
The source must be a non-empty sequence of base- radix digits, optionally prefixed with a minus or plus sign ('-' or '+').
You can specify an onError named argument in your call to parse, which takes a callback that handles the invalid input. E.g., if you want the parse call to return the value 42 for all invalid input, you can do this:
usrLoc = int.parse(query("#txtLoc").text, onError: (val) => 42);
If you really expect the element to have some text, you can store the result of query("#txtLoc").text into a separate variable and verify the value. It would also be interesting to check what the real element type is or which tag is marked with id #txtLoc.
If you want to get the content of an input element, you should use the value property instead of text:
query("#txtLoc").value

Resources