Count objects in array for string - ios

I have an array stored in Parse and I wanted to count the objects in the array and then display that number in a label. I have been able to count the objects but for some reason can not figure out how to turn it into a string. Here is my code:
cell.CheckinLabel.text = (#"count = %d", [[imageObject objectForKey:#"Checkin"] count]);
NSLog(#"count = %d", [[imageObject objectForKey:#"Checkin"] count]);
Any help would be appreciated.

Try this:
NSString *string = [NSString stringWithFormat:#"%d", [[imageObject objectForKey:#"Checkin"] count]];
yourlabel.text = string;
Or,
yourlabel.text = [NSString stringWithFormat:#"%d", [[imageObject objectForKey:#"Checkin"] count]];

Related

IOS:Objective-C: How to combine elements of arrays

I have an array of people objects stored in Core Data and I would like to create a string made by combining two attributes of each, as in firstname and lastname.
NSArray *firstArr = [[Employees valueForKey:#"first"] allObjects];
NSArray *secondArr = [[Employees valueForKey:#"last"] allObjects];
I can create a string from one attribute such as "John,Jane" with
NSString *firststr = [firstarr componentsJoinedByString:#","];
Can anyone suggest how to make a string that has the first and last names separated by commas as in John Doe, Jane Doe? Imagine there is a method but I cannot find it.
You can do it like this
NSArray *firstArr = #[#"Rajat" , #"Rajat1", #"Raj"];
NSArray *secondArr = #[#"Test" , #"Test1", #"Test2"];
NSString * str = #"";
for (int i=0; i< (firstArr<secondArr ? firstArr.count:secondArr.count); i++) {
str = [str stringByAppendingString:[NSString stringWithFormat:#"%# %#,",firstArr[i],secondArr[i]]];
}
str = [str substringToIndex:[str length]-1];
NSLog(#"%#",str);

Insert elements in next index of NSMutableArray

I am adding multiple elements in NSMutableArray but its storing single element at o index. I need to add the elements into next index every time until the end of the loop
for (i=1; i<58; i++) {
NSString *stringFromInt = [NSString stringWithFormat:#"%d", i];
NSString *strlastName =[[dict objectForKey:stringFromInt]objectAtIndex:7];
[arrLastName insertObject:strlastName atIndex:i];
}
This particular code should throw NSRangeException. You can't insert the item at index greater than array count minus one.
I think you want to add elements to array untill the loop runs, The problem might be with i, which is not initialized
for (int i=1; i<58; i++) {
NSString *stringFromInt = [NSString stringWithFormat:#"%d", i];
NSString *strlastName =[[dict objectForKey:stringFromInt] objectAtIndex:7];
[arrLastName insertObject:strlastName atIndex:i]; // or try [arrLastName addObject:strlastName];
}
Hope this helps

Parse all integers from string in Objective-C

I have a problem how to get all integer values from string in Objective-C
NSString *numbers = #"1, 2";
int number = [numbers intValue];
But this just takes the first number (1) but I need both of them.
Thank you guys.
Try something like this:
NSArray *listOfNumbers = [numbers componentsSeparatedByString:#","];
for (NSString *numberAsString in listOfNumbers) {
int number = [numberAsString intValue]; // you might want to trim the string first
}
This is for if they're always separated by a ", ":
NSString *numbers = #"1, 2";
NSArray *numberTokens = [numbers componentsSeparatedByString:#", "];
for (NSString *token in numberTokens) {
NSLog(#"%i", token.integerValue);
}
This solution allows you to specify multiple characters that might separate the numbers:
NSString *numbers = #"1, 2";
NSArray *numberTokens = [numbers componentsSeparatedByCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:#", "]];
for (NSString *token in numberTokens) {
if (token.length > 0) {
NSLog(#"%#: %i", token, token.integerValue);
}
}

iOS: Display NSArray of NSStrings in a label with special character encoding

I have several NSStrings which I add to an NSArray. The string may contain special characters. In the end I want to print the array to a UILabel.
The very simplified code (if you think I missed something, let me know):
NSString *strBuffer = #"Röckdöts";
NSLog(#"String: %#", strBuffer);
NSMutableArray *marrSelectedStrings = [[NSMutableArray alloc] init];
[marrSelectedStrings addObject:strBuffer];
NSLog(#"Array: %#", marrSelectedStrings);
NSUInteger iCount = [marrSelectedStrings count]
for (NSUInteger i = 0; i < iCount; i++)
{
NSLog(#"element %d: %#", i, [marrSelectedStrings objectAtIndex: i]);
}
In a different UIViewController:
self.label.text = [NSString stringWithFormat:#"%#", marrSelectedStrings];
The string itself prints out fine. For the array however, it depends on the output method, whether the console diplays the correct special character or code for it. The label only prints code instead of the real characters. The print out via NSLog looks like the following:
Buffer: Röckdöts
Array: (
R\U00f6ckd\U00f6ts
)
element 0: Röckdöts
Whereas the label reads:
R\U00f6ckd\U00f6ts
I tried using stringWithUTF8String during the adding to the array as well as encoding during assigning it to the label like so, but it didn't change the result:
// adding with UTF8 encoding
[marrSelectedStrings addObject:[NSString stringWithUTF8String:[strBuffer UTF8String]]];
// printing to label with UTF8 encoding
self.label.text = [NSString stringWithUTF8String:[[NSString stringWithFormat:#"%#", marrSelectedStrings] UTF8String]];
Is there an easier way to simply print the array with correct character encoding to the UILabel than iterating over the array and appending every single word?
Try this
NSString * result = [[marrSelectedStrings valueForKey:#"description"] componentsJoinedByString:#""];
self.label.text = result;
try like this
NSMutableArray *marrSelectedStrings = [[NSMutableArray alloc] init];
[marrSelectedStrings addObject:strBuffer];
NSString *description = [marrSelectedStrings description];
if (description) {
const char *descriptionChar = [description cStringUsingEncoding:NSASCIIStringEncoding];
if (descriptionChar) {
NSString *prettyDescription = [NSString stringWithCString:descriptionChar encoding:NSNonLossyASCIIStringEncoding];
if (prettyDescription) {
description = prettyDescription;
}
}
}
NSLog(#"Array: %#", description);

Converting an NSArray component into an integer or decimal number

I have a case where the data read from the CSV file in the app has to be converted into an integer, has to be plotted later. Currently it doesn't recognize when the data is saved as
int i=[[rows objectAtIndex:0] componentsSeparatedByString:#","];
This is the implemented code.
-(void)connection :(NSURLConnection *) connection didReceiveData:(NSData *)data{
[self serverConnect];
response = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
//NSLog(response);
NSString *stripped1 = [response stringByReplacingOccurrencesOfString:#"\r" withString:#""];
NSArray *rows = [stripped1 componentsSeparatedByString:#"\n"];
NSArray *components;
for (int i=0;i<[rows count]; i++) {
if(i == 0 || [[rows objectAtIndex:i] isEqualToString:#""]){
continue;
}
components = [[rows objectAtIndex:i] componentsSeparatedByString:#","];
NSLog(#"data1:%# data2:%# data3:%#", [components objectAtIndex:0] ,[components objectAtIndex:1],[components objectAtIndex:2]);
}
data1, data2 and data3 are supposed to be integers.
Thanks a lot.
componentsSeparatedByString returns substrings, or instances of NSString.
components = [[rows objectAtIndex:i] componentsSeparatedByString:#","];
You just need to take each member of 'components' and get it's intValue, like so:
int myInt = [[components objectAtIndex:n] intValue];
NSArray and NSMutableArray can only contains objects. So get the integer value from it, use [object intValue]. If you need to add an integer to an array, create a NSNumber object from the integer and insert it. I know Rayfleck answered your question and i just want to point out the way how array works in iOS. Hope this helps.

Resources