Objective-C iOS parse string according to the definition - ios

I want create an iOS app for my school.
This App will show Week schledule and when I tap on Cell with Subject, it will show me detail info about subject...
My problem:
Our teachers use shotcuts for their names, but I want show their full name... I created the file "ucitele.h" with definitions of their names, but I don't know, how to use it 😕.
This is how that file looks:
//
// ucitele.h
//
#define Li #"RNDr. Dan---vá"
#define He #"Mgr. Ja---hl"
#define Sm #"Ing. Mich---rek"
#define Ks #"Mgr. Svat---á"
I get the shortcut of Teacher from previous view from "self.ucitel" and I maybe want compare the contents of the "self.ucitel" with definitions and set the "ucitelFull" string from the definitions? I don't know how to say it 😕.
when the content of the self.ucitel will be #"Sm", than I want parse "ucitelFull" as #"Ing. Mich---rek"
Answers in Objective-C only please

Okay, sounds like your trying to map a short identifier to a full name:
-(NSString*)fullNameFromShortName:(NSString*)short {
NSDictionary * names = #{#"Li" : #"RNDr. Dan---vá",
#"He" : #"Mgr. Ja---hl", ... };
return [names objectForKey:short];
}
Use like:
self.ucitelFull = [self fullNameFromShortName:self.ucitel];
This is a dictionary that has the short name as a key and the full name as the value.
Some further suggestions:
try using lowercase keys and comparing lowercaseString's, incase the user doesn't enter the value with the correct case.
You can move the dictionary definition into a json file and read it from your bundle, to eliminate the hardcoding

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.

LUA : Use table from another file

I just studied Lua language. I am very confused about it.
I have two file
string.lua
STRINGS=
{
CHARACTER_NAMES =
{
web = "webby",
sac = "sacso",
}
}
STRINGS.BUNNYNAMES =
{
"Brassica",
"Bunium",
"Burdock",
"Carrot",
}
And I have generate.lua file for get value form table in string.lua.
Then print value.
But I don't know how to acess table form another file.
I want to use STRINGS and STRINGS.BUNNYNAMES too,
Please advise me.
string.lua defines one global variable named STRINGS which contains a table.
You need to execute string.lua before you can access STRINGS. Just do dofile("string.lua") for instance.

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.

UILexicon iOS 8 is not working as expected

I am using UILexicon for the suggestions in custom keyboard. Following is code:
-(void) keyTapped:(UIButton*)button {
[self requestSupplementaryLexiconWithCompletion:^(UILexicon *lexicon){
// self.lexicon = lexicon;
NSLog(#"%#",lexicon.entries);
for (UILexiconEntry* entry in lexicon.entries) {
NSLog(#"%#=%#",entry.userInput,entry.documentText);
}
int i=0;
}];
}
But it is returning always same array of entries. Can anyone suggest me how to use it. I will mark correct your answer if it works. Thanks.
It's working but you should implement your own function to compare the UILexiconEntry list with the entered string as stated by apple :
UILexiconEntry
A lexicon entry specifies a read-only term pair, available within a UILexicon object, for use by a custom keyboard.
You can employ a lexicon entry by matching user input against the entry’s userInput value, and then inserting into the current text input object the corresponding documentText value. For example, if the user typed the string “iphone”, the lexicon entry with that exact, case-sensitive string in the userInput property has the string “iPhone” in the corresponding documentText property.
In some cases, the documentText string is in a different text script than the userInput string.

Reading strings from a text file in Objective-C

The game is similar to the quiz game. Questions are pictures and answers are strings.
Just wondering what would be the best way to read strings(answer) from the text file randomly, in order to use the string selected to pull up pictures(questions) from a set of pictures. Pictures will have the same names as all the name strings in the text file, however I can't have them repeat.
As of now I have switch statement that has multiple cases that select the picture(question) and strings(answers). Basically I don't want to keep all the strings in code in a .m file.
The question will be in a form of a picture and a text file will hold answers.
answers.txt
gta
fifa
minecraft
Questions:
gta.jpg
fifa.jpg
minecraft.jpg
so the randomizer will for example pick
answer gta
and when it does so, it should select the right pic(gta.jpg)
so at the end it will look like this:
gta.jpg
and four answers choices including the gta and the player will pick the right answer
is this clear?
Use a property list. Store the list of questions as an array of dictionaries, where each dictionary has entries for the question file name and the answer, like this:
[
{
"question" : "gta.jpg",
"answer" : "gta"
},
{
"question" : "fifa.jpg"
"answer" : "fifa"
},
//...
]
Then you can read the dictionary into memory using a convenience method:
NSArray *questions = [NSArray arrayWithContentOfFile:pathToQuestionsPList];
I think Caleb's suggestion is very good. By having an array of dictionaries, string and it's image are always kept together.
You could write a one-time parser method that would take a text file as input and generate your output plist. I'm thinking your file would be multiple lines of imageNameanswer. You'd then read the text file, use the NSString method componentsSeparatedByString to break it up into lines by line break, and then loop through the lines, again using componentsSeparatedByString: #"\i" (the tab character), this time to break it into a filename and an answer string. You turn the results into an array of dictionaries and write it out to your app's documents folder. Then just drag the result into your project.
If you wanted to get really fancy you could turn your text file parser into a command-line tool, and make it part of the build process so that when you update your text file of image names and answers, the build process automatically runs the parser on it and copies the output into the application bundle. Methinks that's a little beyond your current abilities however.
If you have a lot of questions you probably want to learn to use core data and a database. If you only have a few, then the plist or dictionary method will work.
One of my apps has a bunch of stories in a database and at the last minute, we decided to add images. Rather than messing with the database, I wrote a quick class that uses a dictionary to pair the story with the an image name.
The view controller queries the class to get the name of the image.
NSString *imageName = [EarlyReadingImageNames findStoryImage:title];
This is the full class.
//
// EarlyReadingImageNames.m
// Words
//
// Created by John Scarry on 5/20/14.
//
#import "EarlyReadingImageNames.h"
#implementation EarlyReadingImageNames
+(NSString *) findStoryImage:(NSString *)story {
NSDictionary* imageDictionary= #{ #"Alice the Worker Bee" : #"Alice",
#"Alice Learns to Fly" : #"Alice",
#"Alice Loves Her New Job" : #"Alice",
#"George Likes to Sing" : #"George",
#"George Likes to Dance" : #"George",
#"George Saves the Day" : #"George",
#"Jensen Meets Bob the Buffalo" : #"JensenBob",
#"Jensen and Bob Play in the Pond" : #"JensenBob",
#"Jensen and Bob Make a Pair of Boots" : #"JensenBob",
#"Rita Finds a New Home" : #"Rita",
#"Rita Makes a Boat" : #"Rita",
#"Rita Loves Words" : #"Rita",
#"The Rock That Looked Like a Frog" : #"Sandy",
#"The Rock and the Rainbow" : #"Sandy",
#"Sandy Makes New Friends" : #"Sandy",
#"James and the Bowl of Baseballs" : #"James",
#"James and the Garden" : #"James",
#"James Builds a Bird House" : #"James",
#"Lily Finds Eggs" : #"Lily",
#"Lily and Bessie the Cow" : #"Lily",
#"Lily Feeds the Lambs" : #"Lily",
#"Hector and Bo" : #"Hector",
#"Hector Loves Fish Socks" : #"Hector",
#"Hector Makes a Kite" : #"Hector",
#"Yoshi and Toshiko Get a New Home" : #"ToshikoYoshi",
#"Yoshi and Toshiko Go to the Library" : #"ToshikoYoshi",
#"Yoshi and Toshiko Go to the Park" : #"ToshikoYoshi",
#"Pete Loves Birds" : #"Pete",
#"Pete Meets Max" : #"Pete",
#"Pete and Max Are Best Friends" : #"Pete"
};
return [imageDictionary valueForKey:story];
}
#end

Resources