how to use 'string' variable as 'objectForkey' in iphone - iphone

I want to use string as objectForkey. my code line is this.
And I want to use "strend" as a objectForkey but it didn't work.
Code is:
NSString *strend = [[NSString alloc] initWithFormat:#"%#",[[detwec.arrdetails objectAtIndex:0]valueForKey:#"EndDate"]];
NSString *dateString = [[NSString alloc]initWithFormat:#"%#",[_currentItemValue objectForKey:#"strend" ]];

try to replace this line
NSString *dateString = [[NSString alloc]initWithFormat:#"%#",[_currentItemValue objectForKey:#"strend" ]];
by this
NSString *dateString = [[NSString alloc]initWithFormat:#"%#",[_currentItemValue objectForKey:strend]];

if you want to read a value using string as key you can use:
NSString *value = ["Your Dictionary" objectForKey:strend];
get a check that your NSString *strend shouldn't be "nil" else will not give you any value.

You Can Try
NSString *strend = [NSString stringWithFormat:#"%#",[detwec.arrdetails objectAtIndex:0]valueForKey:#"EndDate"];
NSString *dateString = [NSString stringWithFormat:#"%#",[_currentItemValue objectForKey:#"strend" ]];
or else
NSString *dateString = [_currentItemValue objectForKey:#"strend" ];

NSString *strend = [NSString stringWithFormat:#"%#",[detwec.arrdetails objectAtIndex:0]]; NSUserDefaults *storeData=[NSUserDefaults standardUserDefaults];
[storeData setObject:strend forKey:#"strend"];
NSString *dateString = [storeData objectForKey:#"strend"];

Related

I want to show the UILabel text "sunday" to uppercase "SUN" like that

NSString *strDay = [dic objectForKey:#"day"];
NSString *uppercaseString = [strDay uppercaseString];
cell.dayLabel.text = uppercaseString;
Is that the correct method to get that? But I get only uppercase. I want "sunday" to be shown in view like "SUN".
How about just this
NSString *uppercaseString = [strDay uppercaseString];
cell.dayLabel.text = [uppercaseString substringToIndex:3];
Assuming it has a valid day
NSString *strDay = [dic objectForKey:#"day"];
NSString* split = [strDay substringToIndex:3];
split=[split uppercaseString];
NSLOG (#"%#",split);
Cheers!
NSString *strDay = #"sunday";
NSString *newStr = [strDay substringToIndex:3];
NSString *uppercaseString = [newStr uppercaseString];
NSString *strDay = [dic objectForKey:#"day"];
cell.dayLabel.text = [strDay.uppercaseString substringToIndex:3]

Reading a Text file in xcode

First off, I'm a complete beginner.
This might be a stupid question, but here it goes:
I'm currently working on an App than contains Latin texts that the users can view and read.
I'm using Xcode 4 with the storybord function.
Theway the app is built: user selects author - then the book - then app shows the text.
I am kind of confused because i need to have various text files, depending on the users choice.
As i have done or do usually just embedded the files as resource and by using the below code I read-out the files
in .h file
-(NSString *)readFile:(NSString *)fileName;
in .m file
-(NSString *)readFile:(NSString *)fileName
{
NSLog(#"readFile");
NSString *appFile = fileName;
NSFileManager *fileManager=[NSFileManager defaultManager];
if ([fileManager fileExistsAtPath:appFile])
{
NSError *error= NULL;
NSString *resultData = [NSString stringWithContentsOfFile: appFile encoding: NSUTF8StringEncoding error: &error];
if (error == NULL)
return resultData;
}
return NULL;
}
May this will help you
Enjoy Coding :)
EDIT
What I have done is according to the date change on one button click and also at the time of viewWillApper I call the function below
-(void)dateAndContentReload
{
NSLog(#"dateAndContentReload");
NSLog(#"Appdelegate Date: %#",appDelegate.dateSelected);
self.currentDate = appDelegate.dateSelected;
NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"dd MMMM yyyy"];
NSString *dateString = [dateFormatter stringFromDate: appDelegate.dateSelected];
self.navigationItem.title = dateString;
[dateFormatter setDateFormat: #"Myyyy"];
NSString *dateComp = [dateFormatter stringFromDate: appDelegate.dateSelected];
//NSLog(#"date : %#",dateString);
NSString *data;
NSString *path;
if(![dateComp isEqualToString: #"12012"])
{
NSString *size = [NSString stringWithFormat: #"%f",appDelegate.size] ;
NSLog(#"dateAndContentReload size: %f",appDelegate.size);
data = #"<html><body><font size='";
data = [data stringByAppendingString: size];
data = [data stringByAppendingString: #"'><P>"];
data = #"Data not Available for Current Date.";
data = [data stringByReplacingOccurrencesOfString: #"\n" withString:#"<br>"];
data = [data stringByAppendingString:#"</P></font></body></html>"];
}
else
{
[dateFormatter setDateFormat:#"MMMMd"];
dateString = [dateFormatter stringFromDate: appDelegate.dateSelected];
[dateFormatter release];
NSLog(#"new Format: %#",dateString);
path = [[NSBundle mainBundle] pathForResource: dateString ofType: #"txt"];
data = nil;
NSString *size = [NSString stringWithFormat: #"%f",appDelegate.size] ;
NSLog(#"dateAndContentReload size: %f",appDelegate.size);
data = #"<html><body><font size='";
data = [data stringByAppendingString: size];
data = [data stringByAppendingString: #"'><P>"];
data = [data stringByAppendingString: [self readFile: path]];
data = [data stringByReplacingOccurrencesOfString: #"\n" withString:#"<br>"];
data = [data stringByAppendingString:#"</P></font></body></html>"];
}
[self.currentView loadHTMLString: data baseURL: nil];
}
Basically I read file and display in UIWebView as client request it.

iPhone:Convert NSString 123-123-1234 into (123) 123-1234

I have string like 123-123-1234 so I want to convert string into this format
(123) 123-1234 so any idea to develop this functionality.
Thanks in advance.
I think something like this:
NSString *list = #"123-123-1234";
NSArray *listItems = [list componentsSeparatedByString:#"-"];
NSString *result = [NSString stringWithFormat:#"(%#) %#-%#", [listItem objectAtIndex:0], [listItem objectAtIndex:1], [listItem objectAtIndex:2]];
NSString * original = #"123-123-1234";
NSArray * components = [original componentsSeparatedByString:#"-"];
NSString * first = [NSString stringWithFormat:#"(%#)",[components objectAtIndex:0] ];
NSString * second = [NSString stringWithFormat:#"%#-%#",[components objectAtIndex:1],[components objectAtIndex:2]];
NSString * finalString = [NSString stringWithFormat:#"%#%#",first,second];
NSLog(#"Final Result = %#",finalString);
NSString *str = #"123-123-1234";
NSArray *arrForDate = [str componentsSeparatedByString: #"-"];
NSString *str1 = [NSString stringWithFormat:#"(%#) %#-%#",[arrForDate objectAtIndex:0],[arrForDate
objectAtIndex:1],[arrForDate objectAtIndex:2]];
NSLog(#"str1 %#",str1);
TESTED CODE: 100% WORKS
NSString *inputString=#"123-123-1234";
NSArray *TotalString=[inputString componentsSeparatedByString:#"-"];
NSString *outputString = [NSString stringWithFormat:#"(%#) %#-%#",[TotalString objectAtIndex:0],[TotalString objectAtIndex:1],[TotalString objectAtIndex:2]];
NSLog(#"outputString is : %# \n\n",outputString);
OUTPUT:
outputString is : (123) 123-1234
For Dynamic Sollution:
NOTE: if u have more dashes with ur string then it is really hard code everything with objectAtIndex with number 0,1,2,....
moreover it will crash if it has unexpected length
so here is the solution for this
NSString *inputString=#"123-123-1234";
NSArray *TotalString=[inputString componentsSeparatedByString:#"-"];
NSMutableString *outputString=[NSMutableString string];
for (NSMutableString *obj in TotalString) {
if ([TotalString objectAtIndex:0] == obj && [outputString length]<=0) {
outputString=[[outputString stringByAppendingFormat:#"(%#)",obj] mutableCopy];
}
else if ([TotalString objectAtIndex:1] == obj) {
outputString=[[outputString stringByAppendingFormat:#"%#%#",#" ",obj] mutableCopy];
}
else {
outputString=[[outputString stringByAppendingFormat:#"-%#",obj] mutableCopy];
}
}
OUTPUT:
outputString is : (123) 123-1234
NSString *list = #"123-123-1234";
NSArray *listItems = [list componentsSeparatedByString:#"-"];
NSString *result = [NSString stringWithFormat:#"(%#) %#-%#", [listItem objectAtIndex:0], [listItem objectAtIndex:1], [listItem objectAtIndex:2]];

how can I convert string to an array with separator?

I have a string in the following format
myString = "cat+dog+cow"
I need to store each string separated by + in to a array.
Eg:
myArray[0] = cat
myArray[1] = dog
myArray[2] = cow
Can anyone tell me the proper way to do this?
componentsSeparatedByString: splits the string and return the result in an array.
NSArray *myArray = [myString componentsSeparatedByString:#"+"];
[myArray objectAtIndex:0];//cat
[myArray objectAtIndex:1];//dog
[myArray objectAtIndex:2];//cow
Try this..
NSArray *arr = [myString componentsSeparatedByString:#"-"];
[arr objectAtIndex:0];//Hai
[arr objectAtIndex:1];//Welcome
It is vert simple..
NSString * test = #"Hello-hi-splitting-for-test";
NSArray * stringArray = [test componentsSeparatedByString:#"-"];
// Now stringArray will contain all splitted strings.. :)
Hope this helps...
I you don't want use array then iterate through each character...
NSMutableString * splittedString = nil;
for(int i=0;i<test.length;i++){
unichar character = [test characterAtIndex:0];
if (character=='-') {
if (splittedString!=nil) {
NSLog(#"String component %#",splittedString);
[splittedString release];
splittedString = nil;
}
} else {
if (splittedString==nil) {
splittedString = [[NSMutableString alloc] init];
}
[splittedString appendFormat:#"%C",character];
}
}
if (splittedString!=nil) {
NSLog(#"String last component %#",splittedString);
[splittedString release];
splittedString = nil;
}
Thats all...
NSArray *myWords = [myString componentsSeparatedByString:#"+"];
You can find this one very simple
NSString *str = #"cat+dog+cow";
NSArray *arr = [str componentsSeparatedByString:#"+"];
NSLog(#"Array items %#",arr);
OUTPUT:
Array items
(
Cat,
dog,
Cow
)
Use the componentsSeparatedByString: method of NSString.
NSString string = #"hai-welcome";
NSArray myArray = [string componentsSeparatedByString:#"-"];
NSString* haiString = [myArray objectAtIndex:0];
NSString* welcomeString = [myArray objectAtIndex:1];
NSArray *strArray = [myString componentsSeparatedByString:#"-"];
firstString = [strArray objectAtIndex:0];//Hai
secondString = [strArray objectAtIndex:1];//Welcome
This will be the solution if you are dealing with a string:
NSString *mySstring = #"hai-welcome";
NSMutableArray *anArray=[[NSMutableArray alloc] initWithArray:[componentsSeparatedByString: #"-"]];
And each word will be stored in respective position from 0-n.
Try This. :)
If you are averse to using arrays, you can consider this –
NSString *accessMode, *message;
NSScanner *scanner = [NSScanner scannerWithString:#"hai-welcome"];
NSCharacterSet *hyphenSet = [NSCharacterSet characterSetWithCharactersInString:#"-"];
[scanner scanUpToCharactersFromSet:hyphenSet intoString:&accessMode];
[scanner scanCharactersFromSet:hyphenSet intoString:nil];
[scanner scanUpToCharactersFromSet:[NSCharacterSet characterSetWithCharactersInString:#""] intoString:&message];
NSArray *lines = [string componentsSeparatedByString:#"-"];
The first value will be stored in 0th index of lines and second value will be stored in 1th index of lines..
Why not array?
Simple code :
NSString *myString = [[NSString alloc] initWithString:#"cat+dog+cow"];
NSArray *resultArray = [tempString componentsSeparatedByString:#"+"];
NSLog(#"1. %# 2. %# 3. %#",[resultArray objectAtIndex:0],[resultArray objectAtIndex:1],[resultArray objectAtIndex:2]);
Try This:
NSString *str = #"cat+dog+cow" ;
NSArray *array = [str componentsSeparatedByString:#"+"];
NSLog(#"%#",array) ;

AES test on iphone

my encryption is working but i cant decrypt kindly suggest what i am doing wrong here
NSString *passphrase = #"hello";
NSStringEncoding myEncoding = NSASCIIStringEncoding;
NSString *alphaStringPlain = #"cell";
NSData *alphaDataPlain = [alphaStringPlain dataUsingEncoding:myEncoding];
NSData *alphaDataCypher = [alphaDataPlain AESEncryptWithPassphrase:passphrase];
NSString *alphaStringCypher = [[NSString alloc] initWithData:alphaDataCypher encoding:myEncoding];
NSLog(alphaStringCypher); // perfeclty encypted i guess
/////// FOR DECRYPTION///////////////
NSData *zCypher = [alphaDataPlain AESDecryptWithPassphrase:alphaStringCypher];
NSString *Cypher = [[NSString alloc] initWithData:zCypher encoding:myEncoding];
NSLog(#" decode %#",[Cypher dataUsingEncoding:NSUTF8StringEncoding]);
NSLog(#" decode %#",Cypher);// not working some garbage value
After struggling i got the ans
NSString *passphrase = #"1234567812345678";
NSStringEncoding myEncoding = NSASCIIStringEncoding;
NSString *alphaStringPlain = #"hello";
NSData *alphaDataPlain = [alphaStringPlain dataUsingEncoding:myEncoding];
NSData *alphaDataCypher = [alphaDataPlain AESEncryptWithPassphrase:passphrase];
NSString *alphaStringCypher = [[NSString alloc] initWithData:alphaDataCypher encoding:myEncoding];
NSLog(alphaStringCypher);
///////
NSData *zCypher = [alphaDataCypher AESDecryptWithPassphrase:passphrase];
NSString *Cypher = [[NSString alloc] initWithData:zCypher encoding:myEncoding];
//NSData *zCypher = [alphaStringCypher AESDecryptWithPassphrase:passphrase];
NSLog(#" hua kya decode %#",cypher);// working
I think you are decrypting wrong value
try with this :
NSData *zCypher = [alphaStringCypher AESDecryptWithPassphrase:passphrase];