how to reveal all string padding from txt file in Xcode? - ios

I making a word game so Ive set all the word in a .txt file, and i've added a show 1st letter button:
Code:
if ([buttonTitle isEqualToString:#"reveal"]) {
UIAlertView *alertuser;
NSString *prla = [NSString stringWithFormat:#"%#", [lst_word objectAtIndex:nCurrentWord]];
NSString *finished = [[prla substringToIndex:1] stringByPaddingToLength:prla.length withString: #"_" startingAtIndex:0];
alertuser = [[UIAlertView alloc]initWithTitle:#"This is the first letter:" message:finish delegate:self cancelButtonTitle:#"Thanks" otherButtonTitles:nil, nil];
[alerta show];
How would I make it show and reveal all the letters from the word in the .txt file?
NSString *finishText = [[prlt substringToIndex:1] stringByPaddingToLength:prlt.length withString: #"_" startingAtIndex:0];
alerta = [[UIAlertView alloc]initWithTitle:#"First Letter is:" message:finishText delegate:self cancelButtonTitle:#"Thanks" otherButtonTitles:nil, nil];
[alerta show];
I need this reversed to allow the full answer from the txt file!

substringWithRange: may be the function you are looking for.
The sample code below will print word by word of a string
NSString *sampleString = #"abcedefgh";
for(int i = 0; i < [sampleString length]; i++)
{
NSLog(#"word = %#", [sampleString substringWithRange:NSMakeRange(i, 1)])
}
EDIT:
In place of
NSString *finishedText = [[prlt substringToIndex:1] stringByPaddingToLength:prlt.length withString: #"_" startingAtIndex:0];
use below code, this will solve your issue
NSString *finishedText= [prlt substringWithRange:NSMakeRange(0, prla.length)];

Related

stringByAppendingPathComponent adds a '/' character to the text

I have the following code:
NSString *message;
if (some_condition) {
message = #"String 1. ";
} else {
message = #"String 2. ";
}
message = [message stringByAppendingPathComponent:#"Bla bla bla."];
UIAlertController *alert = [UIAlertController alertControllerWithTitle:#"Alert"
message:message
preferredStyle:UIAlertControllerStyleAlert];
The text that I see when the alert pops up has an extra '/' character where the string were appended:
Where did it come from and how do I remove it?
You are using the wrong method there.
Instead of :
message = [message stringByAppendingPathComponent:#"Bla bla bla."];
Use:
message = [message stringByAppendingString:#"Bla bla bla."];
When you use stringByAppendingPathComponent: it will return a new string made by appending the provided string to the receiver, preceded if necessary by a path separator.
Reference : NSString Class Reference

Data Not inserted In Sqlite objective c

I want to insert some values in a table of database.but it is showing (NULL) on insertion
I am using This code
http://pastie.org/10929618
I have used this Link for Reference
http://www.tutorialspoint.com/ios/ios_sqlite_database.htm
//Insert Method
-(void)SaveAction{
BOOL success = NO;
NSString *alertString = #"Data Insertion failed";
success = [[DBManager getSharedInstance]saveData:#"100" username:#"byname" type:#"one2one" user_to:#"144" user_from:#"145" timestamp:#"42015"];
if (success == NO) {
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:
alertString message:nil
delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alert show];
}
}
You are missing the column value.
// you are passing the value use below method but dnt have "message1" column value
success = [[DBManager getSharedInstance]saveData:#"100" username:#"byname" type:#"one2one" user_to:#"144" user_from:#"145" timestamp:#"42015"];
// compare the below query
NSString *insertSQL = [NSString stringWithFormat:#"insert into chatDetail (chat_id,username, type, message1,user_to,user_from,timestamp) values (\"%ld\",\"%#\", \"%#\", \"%#\",\"%#\",\"%#\")",(long)[chat_id integerValue], username, type, user_to,user_from,timestamp];

Fetching and comparing CFBundleVersion from plist

I'm trying to compare CFBundleVersion key of 2 Apps inside com.apple.mobile.installation.plist which include the info of every installed application on iPhone
NSString *appBundleID =#"net.someapp.app";
NSString *appBundleID2=#"net.someapp.app2";
NSDictionary *dict = [[NSDictionary alloc] initWithContentsOfFile:
#"/var/mobile/Library/Caches/com.apple.mobile.installation.plist"];
NSDictionary *User = [dict valueForKey:#"User"];
//get first app version
NSDictionary *bundleID = [User valueForKey:appBundleID];
NSString *appVersion = [bundleID valueForKey:#"CFBundleVersion"];
//get second app version
NSDictionary *bundleID2 = [User valueForKey:appBundleID2];
NSString *appVer2 = [bundleID2 valueForKey:#"CFBundleVersion"];
[dict release];
if ([appVersion isEqualToString:appVer2]) {
NSString *str1=[NSString stringWithFormat:#"Original Version: %#",appVersion];
NSString *str2=[NSString stringWithFormat:#"2nd Version: %#",appVer2];
NSString *msg=[NSString stringWithFormat:#"%#\n%#",str1,str2];
UIAlertView* alertView = [[UIAlertView alloc]
initWithTitle:#"Same Versions!" message:msg delegate:nil
cancelButtonTitle:#"Ok" otherButtonTitles:nil];
[alertView show];
}
else {
NSString *str1=[NSString stringWithFormat:#"Original Version: %#",appVersion];
NSString *str2=[NSString stringWithFormat:#"2nd Version: %#",appVer2];
NSString *msg=[NSString stringWithFormat:#"%#\n%#",str1,str2];
UIAlertView* alertView = [[UIAlertView alloc]
initWithTitle:#"Different Versions!" message:msg delegate:nil
cancelButtonTitle:#"Ok" otherButtonTitles:nil];
[alertView show];
}
The version of both apps is currently set to 2.11.8
I am getting the following wrong result:
If i set the NSString manually:
NSString *appVersion =#"2.11.8";
NSString *appVer2 =#"2.11.8";
i get the correct desired result:
I also tried other ways to compare the strings but the result was always the same, so i guess the problem is with fetching the values of the keys?
Any help is appreciated
I am so used to ARC that I am not 100% sure about the MRC rules anymore. But I assume
that you either have to retain the values appVersion and appVer2 from the dictionary,
or alternatively, postpone the [dict release] until after the values are no longer needed.
Since you don't own the values fetched from the dictionary, they become invalid if the
dictionary is released.
(This would not be a problem if you compile with ARC!)
Remark: The designated method to get a value from a dictionary is objectForKey:.
valueForKey: works also in many cases, but can be different. It should only be used
for Key-Value Coding magic.

Trying to retrieve only NSStrings excluding whitespace from UITextField in iOS

I am working on an iOS application where a user is required to enter their first and last name into a UITextField. Now, I want my UITextField to be able to check to see that the user has entered:
(1) two names and (2) each name is a minimum of two characters.
My problem is that when I retrieve the name(s) from the text field, I am unable to distinguish between an empty NSString (i.e. " "), and an NSString that is composed of characters. Because of this, when I check to see what the length of the shortest word that has been entered is, the empty spaces are always counted as being valid strings, which I don't want. So in other words, if a user enters:
" larry bird "
I need the string count to be two, and the length of the shortest word to be 4 (thus passing both tests). So I need the leading, trailing whitespaces as well as the whitespaces in between to be removed, but keep the NSString objects that are valid to be distinct.
Here is the code that I have thus far:
NSString *name = [textField text];
NSArray *trimmedArray = [name componentsSeparatedByCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
NSMutableArray *trimmedNames = [NSMutableArray array];
for (NSString *cleanString in trimmedArray) {
NSString *trimmedString = [cleanString stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
NSLog(#"The trimmed name is: %#", trimmedString);
[trimmedNames addObject:trimmedString];
}
self.alert = [[UIAlertView alloc] initWithTitle:#"Error" message:#"Please Enter Full First and Last Name" delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
if(trimmedNames.count > 1) {
NSNumber *lengthOfShortestWord = [trimmedNames valueForKeyPath:#"#min.length"];//here is where I am counting the characters in the smallest string entered the textfield
if (lengthOfShortestWord.intValue > 1) {
[textField resignFirstResponder];
}
else if (lengthOfShortestWord.intValue <= 1) {
[self.alert show];
}
}
else if (trimmedNames.count < 2) {
[self.alert show];
}
Does anyone see what it is that I am doing wrong?
Trim the white space from the initial string from the text field.
NSString *name = [[textField text] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
Now you don't need to trim each individual string.

Game center submission not working

I have an app and am trying to submit scores to game center. this is my code:
- (IBAction) submitScore{
NSString *show = [[NSString alloc] initWithFormat:#"Note: Scores may take some time to update"];
self.note.text = show;
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Submitted"
message:#"Your score has been submitted to the Gamecenter leaderboard"
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles: nil];
[alert show];
array = [NSMutableArray arrayWithContentsOfFile:Path];
NSString *s = [[NSNumber numberWithInteger:[array count]] stringValue];
NSString *denom = [NSString stringWithFormat: #"%d", 15];;
double resultInNum;
double sdouble = [s doubleValue];
double denomdouble = [denom doubleValue];
resultInNum = sdouble/denomdouble * 100;
if(resultInNum > 0)
{
self.currentLeaderBoard = kLeaderboardID;
[self.gameCenterManager reportScore: resultInNum forCategory: self.currentLeaderBoard];
Submit.enabled = NO;
}
array = [NSMutableArray arrayWithContentsOfFile:Path];
[array writeToFile:Path atomically:YES];
NSLog(#"Count: %i", [array count]);
}
however when i try to do it it does not submit to the leader board. there are no errors that i am receiving in the debugger, and it used to work until i added more than one leader board. whats wrong here?
Score value must be an int64_t.
And I don't get why you are using NSStrings instead of directly assigning values?
Just make sure that you do the correct code for submitting the score. This is a way that I really like. Even though you will get warnings because they don't want you to use it in iOS 7, it still works. Just let me know if it doesn't work.
(IBAction)submitscoretogamecenter{
GKLocalPlayer *localplayer = [GKLocalPlayer localPlayer];
[localplayer authenticateWithCompletionHandler:^(NSError *error) {
}];
//This is the same category id you set in your itunes connect GameCenter LeaderBoard
GKScore *myScoreValue = [[[GKScore alloc] initWithCategory:#"insertCategory"] autorelease];
myScoreValue.value = scoreInt;
[myScoreValue reportScoreWithCompletionHandler:^(NSError *error){
//insert what happens after it's posted
}];
[self checkAchievements];
}

Resources