How to display an NSArray in a UITextView - ios

I have a array that's I want to have in a uitextview. This is how it's currently displaying. How do I remove the ( ( ) )'s?
Here's a screenshot
Here's my code:
NSArray *arrayConditions = [[NSArray alloc] initWithObjects:[item objectForKey:#"conditions"], nil];
NSString * resultConditions = [arrayConditions description];
self.conditionsDeal.text = resultConditions;
NSLog(#"conditions are %#", [item objectForKey:#"conditions"]);
thanks for any help

NSArray *arrayConditions = [[NSArray alloc] initWithObjects:[item objectForKey:#"conditions"], nil];
// change this line only for the concatenation
NSString * resultConditions = [arrayConditions componentsJoinedByString:#"\n"];
self.conditionsDeal.text = resultConditions;
// this line always provides you the result of the '-description' method of the 'NSArray'
NSLog(#"conditions are %#", [item objectForKey:#"conditions"]);

NSMutableString *textViewText = [NSMutableString string];
for (NSString *str in arrayConditions)
[textViewText appendFormat:#"%#\n", str];
self.conditionsDeal.text = textViewText;

Convert that array into NSMutableString.
Separate that string by "," or space and write it UITextView.

Related

Fetch Substring from NSString

I have the following string :
Item 1#Item 2#Item 3#Item 4#Item 5#Item 6#Item 7#Item 8#Item 9#Item 10#Item 11#Item 12#Item 13#Item 14#Item 15#
I want to extract each Item name. How can it be done?
try this
NSString *temp = #"Item 1#Item 2#Item 3#Item 4#Item 5#Item 6#Item 7#Item 8#Item 9#Item 10#Item 11#Item 12#Item 13#Item 14#Item 15#";
NSArray *arr = [temp componentsSeparatedByString:#" "];
NSLog(#"%#", arr);
Separate each string
NSString *str = #"Item 1#Item 2#Item 3#Item 4#Item 5#Item 6#Item 7#Item 8#Item 9#Item 10#Item 11#Item 12#Item 13#Item 14#Item 15#";
NSArray *components = [str componentsSeparatedByString:#"#"];
NSString *string=#"Item 1#Item 2#Item 3#Item 4#Item 5#Item 6#Item 7#Item 8#Item 9#Item 10#Item 11#Item 12#Item 13#Item 14#Item 15#";
NSArray *array=[[NSArray alloc] initWithObjects:[string componentsSeparatedByString:#"#"]];

Fetching data from SQLite and want to get only the last value of column id

I am fetching data from SQLite and want to get only the last value of column id in XCode.The code is
NSString *selquery = #"select id from watchlists";
if (self.uid != nil) {
self.uid = nil;
}
self.uid = [[NSArray alloc] initWithArray:[self.dbManager loadDataFromDB:selquery]];
NSString *valvar;
valvar = [_uid lastObject];
NSNumber *custval = [_uid valueForKey: #"#lastObject"];
NSString *imgval1 = [NSString stringWithFormat:#"%#_%s",custval,"1"];
NSLog(#"%#", imgval1);
Please tell me how can I get only the value because by using the above code I am getting array with last value of id.
I think this your case, try this it maybe help you
NSArray *temp=[NSArray arrayWithObjects:#"1",#"2",#"3", nil];
NSArray *temp0ne=[[NSArray alloc]initWithArray:temp];
// NSString *tmmp=[temp0ne lastObject];
NSArray *finalStr=[uid lastObject];
NSLog(#"Dictionary is---->%#",[finalStr lastObject]);
Output:
3_1
EDIT
NSArray *temp=[NSArray arrayWithObjects:#"(1)",#"(2)",#"(3)", nil];
NSArray *temp0ne=[[NSArray alloc]initWithArray:temp];
NSString *tmmp=[temp0ne lastObject];
NSString *final=[tmmp stringByReplacingOccurrencesOfString:#"(" withString:#""];
final=[final stringByReplacingOccurrencesOfString:#")" withString:#""];
NSString *imgval1 = [NSString stringWithFormat:#"%#_%s",final,"1"];
NSLog(#"%#", imgval1);
I don't know is this correct way or not try this....otherwise have look this link
I don't fully understand your code structure hehe. Try this:
NSString *selquery = #"select id from watchlists";
if (self.uid != nil) {
self.uid = nil;
}
self.uid = [[NSArray alloc] initWithArray:[self.dbManager loadDataFromDB:selquery]];
NSNumber *custval = [_uid objectAtIndex:[_uid count]-1];
*
NSString *str = [NSString stringWithFormat#"%#",custval];
str = [str stringByReplacingOccurrencesOfString:#"("
withString:#""];
NSString *finalCustval = [NSString stringWithFormat#"%#",str];
finalCustval = [finalCustval stringByReplacingOccurrencesOfString:#")"
withString:#""];
*
NSString *imgval1 = [NSString stringWithFormat:#"%#_%s",finalCustval ,"1"];
NSLog(#"%#", imgval1);
UPDATE
try adding the ones with *.

Split NSString from first whitespace

I have a name textfield in my app, where both the firstname maybe a middle and a lastname is written. Now I want to split these components by the first whitespace, the space between the firstname and the middlename/lastname, so I can put it into my model.
For example:
Textfield Text: John D. Sowers
String 1: John
String 2: D. Sowers.
I have tried using [[self componentsSeparatedByCharactersInSet:[NSCharacterSet whitespaceCharacterSet]] firstObject]; & [[self componentsSeparatedByCharactersInSet:[NSCharacterSet whitespaceCharacterSet]] lastObject];
But these only work if have a name without a middlename. Since it gets the first and the last object, and the middlename is ignored.
So how would I manage to accomplish what I want?
/*fullNameString is an NSString*/
NSRange rangeOfSpace = [fullNameString rangeOfString:#" "];
NSString *first = rangeOfSpace.location == NSNotFound ? fullNameString : [fullNameString substringToIndex:rangeOfSpace.location];
NSString *last = rangeOfSpace.location == NSNotFound ? nil :[fullNameString substringFromIndex:rangeOfSpace.location + 1];
...the conditional assignment (rangeOfSpace.location == NSNotFound ? <<default value>> : <<real first/last name>>) protects against an index out of bounds error.
Well that method is giving you an array with all the words split by white space, so then you can grab the first object as the first name and the rest of the objects as middle/last/etc
NSArray *ar = [self componentsSeparatedByCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
NSString *firstName = [ar firstObject];
NSMutableString *rest = [[NSMutableString alloc] init];
for(int i = 1; i < ar.count; i++)
{
[rest appendString:[ar objectAtIndex:i]];
[rest appendString:#" "];
}
//now first name has the first name
//rest has the rest
There might be easier way to do this, but this is one way..
Hope it helps
Daniel
I think this example below I did, solves your problem.
Remember you can assign values from the array directly, without transforming into string.
Here is an example:
NSString *textField = #"John D. Sowers";
NSArray *fullName = [textField componentsSeparatedByCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:#" "]];
if (fullName.count)
{
if (fullName.count > 2)
{
NSLog(#"Array has more than 2 objects");
NSString *name = fullName[0];
NSLog(#"Name:%#",name);
NSString *middleName = fullName[1];
NSLog(#"Middle Name:%#",middleName);
NSString *lastName = fullName[2];
NSLog(#"Last Name:%#",lastName);
}
else if(fullName.count == 2)
{
NSLog(#"Array has 2 objects");
NSString *name = fullName[0];
NSLog(#"Name:%#",name);
NSString *lastName = fullName[1];
NSLog(#"Last Name:%#",lastName);
}
else
{
NSString *name = fullName[0];
}
}
I found this to be most robust:
NSString *fullNameString = #"\n Barnaby Marmaduke \n \n Aloysius ";
NSMutableArray *nameArray = [[fullNameString componentsSeparatedByCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] mutableCopy];
[nameArray removeObject:#""];
NSString *firstName = [nameArray firstObject];
if(nameArray.count)
{
[nameArray removeObjectAtIndex:0];
}
NSString *nameRemainder = [nameArray componentsJoinedByString:#" "];
Bob's your uncle.

convert array format according to array character

I have array in this format
rows = [[NSArray alloc] initWithObjects:#"adam", #"alfred", #"ain", #"abdul", #"anastazja", #"angelica",
#"dennis" , #"deamon", #"destiny", #"dragon", #"dry", #"debug" #"drums",
#"Fredric", #"France", #"friends", #"family", #"fatish", #"funeral",
#"Mark", #"Madeline",
#"Nemesis", #"nemo", #"name",
#"Obama", #"Oprah", #"Omen", #"OMG OMG OMG", #"O-Zone", #"Ontario",
#"Zeus", #"Zebra", #"zed", nil];
But i need this in to following format
rows = #[#[#"adam", #"alfred", #"ain", #"abdul", #"anastazja", #"angelica"],
#[#"dennis" , #"deamon", #"destiny", #"dragon", #"dry", #"debug", #"drums"],
#[#"Fredric", #"France", #"friends", #"family", #"fatish", #"funeral"],
#[#"Mark", #"Madeline"],
#[#"Nemesis", #"nemo", #"name"],
#[#"Obama", #"Oprah", #"Omen", #"OMG OMG OMG", #"O-Zone", #"Ontario"],
#[#"Zeus", #"Zebra", #"zed"]];
Means that same starting character in to different dictionary
The easiest approach.
NSArray *rows = ...;
NSMutableDictionary *map = [NSMutableDictionary dictionary];
for (NSString *value in rows) {
NSString *firstLetter = [value substringToIndex:1];
if (!map[firstLetter]) {
map[firstLetter] = #[];
}
NSMutableArray *values = [map[firstLetter] mutableCopy];
[values addObject:value];
map[firstLetter] = values;
}
NSArray *finalRows = [map allValues];
Note that finalRows is not sorted.
If you want to sort your array by it's first letter, you can try this :
NSMutableArray *outputArray = [NSMutableArray new];
NSString *lastFirstLetter = nil;
for(NSString *value in rows) {
NSString *firstLetter = [[value substringToIndex:1] lowerString];
if(![lastFirstLetter isEqualToString:firstLetter]) {
lastFirstLetter = firstLetter;
[outputArray addObject:[NSMutableArray new]];
}
[[outputArray lastObject] addObject:value];
}
The idea is to iterate your input array and if the first letter of your word is different than the precedent, create a new array.

uilabel text with new line only when required

this is what i am using:
it works if address, city, zip.....length >0.(these field may grow in future)
self.addressInfoLbl.text = [NSString stringWithFormat:#"%#\n%#\n%#\n%#\n%#", address, city, zip, state, country];(numberofline == 0)
but if any of them length =0 then i got unnecessary new line.
i am working on manually preparing(appending \n).if there are more and more fields then doing it manuallt is really hard.
Is there any other proper way.Am i doing it right.
Thanks
Try following code. It creates array of your strings, removes empty strings and then concatenates them with componentsJoinedByString :
NSArray *strings = #[address, city, zip, state, country];
strings = [strings filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:#"length > 0"]];
NSString *resultString = [strings componentsJoinedByString:#"\n"];
You can join an array of objects into a string with a separator:
NSArray *props = [NSArray arrayWithObjects: address, city, state, nil];
NSString *joinedString = [props componentsJoinedByString:#"\n"];
and you will get:
"6th avenue\nAtlanta\nGeorgia"
If you don't know the amount of properties, use NSMutableArray instead of NSArray and add your properties at runtime.
Try this once,
NSMutableString *joinedString=[NSMutableString string];
NSArray *arr = [NSArray arrayWithObjects: address, city, state, nil];
for(NSString *str in arr)
{
if([str length]>0) [joinedString appendFormat:#"\n%#", str];
}
NSString *resultString=[joinedString stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
NSLog(#"%#", resultString);
Lbl.numberOfLines=0;
Lbl.lineBreakMode=NSLineBreakByCharWrapping;
try this code, it not optimal but it can resolve youy issue
NSArray *arr = [NSArray arrayWithObjects: #"address", #"", #"state", nil];
NSString *addressInfo = #"";
for (NSString *str in arr) {
if (str.length > 0) {
addressInfo = [addressInfo stringByAppendingString:[NSString stringWithFormat:#"\n%#", str]];
}
}
if (addressInfo && ![#"" isEqualToString:addressInfo])
addressInfo = [addressInfo substringFromIndex:1];
NSLog(#"address Info = %#", addressInfo);

Resources