For Looping through Property in an Array - ios

I'm trying to loop through my array called songs which contains a list of the user's songs from their iPod library, but to get the title, I need to do this (to get an NSString of the song titles):
[[songs objectAtIndex:i] valueForProperty:MPMediaItemPropertyTitle]
I'm trying to create an index of the tableView, but I'm stuck at this bit:
for (NSString *title = MPMediaItemPropertyTitle in songs)
{
rowTitle= [title substringToIndex:1]; //modifying the statement to its first alphabet
if ([rowTitle isEqualToString:sectionTitle]) //checking if modified statement is same as section title
{
[rowContainer addObject:title]; //adding the row contents of a particular section in array
}
}
Where I get an error
-[MPConcreteMediaItem substringToIndex:]: unrecognized selector sent to instance
On this line: rowTitle= [title substringToIndex:1];.
How do I loop through songs to get the MPMediaItemPropertyTitle and then get the first letter of the song title? I thought what I am doing is declaring the NSString 'title and looping through all the titles in songs. Clearly I'm not :S.
I'm following this tutorial. Could anybody help me out, please? Thanks.

for..in loops through the objects in the songs array. It won't send a valueForProperty message automatically, so you'll have to do that yourself:
for (MPMediaItem *song in songs)
{
NSString *title = [song valueForProperty:MPMediaItemPropertyTitle];
rowTitle= [title substringToIndex:1]; //modifying the statement to its first alphabet
if ([rowTitle isEqualToString:sectionTitle]) //checking if modified statement is same as section title
{
[rowContainer addObject:title]; //adding the row contents of a particular section in array
}
}

This...
for (NSString *title = MPMediaItemPropertyTitle in songs)
Should be...
for (MPMediaItem *song in songs) {
NSString *title = [song valueForProperty:MPMediaItemPropertyTitle];
}
Your original code was pointing your title reference at an MPConcreteMediaItem item.

Try this.
for (NSString *title in songs)
{
rowTitle= [title substringToIndex:1]; //modifying the statement to its first alphabet
if ([rowTitle isEqualToString:sectionTitle]) //checking if modified statement is same as section title
{
[rowContainer addObject:title]; //adding the row contents of a particular section in array
}
}

Related

Xcode how to display array inside array in tableview

In Xcode5 I have application of recipes.
screen1 is list of recipes in a tableview. The recipe are objects in an array at MVC model. at Screen1 when clicking on a row you move to screen2 to see a specific recipe Details. One of them is Ingredients which I try to display as array (inside array) in UITextView (divided by \n).
I run the app - screen1 is displayed, but when clicking a row the app crashes at: signal SIGABRT.
Can you please advise what is best way to display array of ingredients in the textview?
Thanks in advance.
This is code at DetailsVC.m (screen2):
-(void)viewWillAppear:(BOOL)animated {
self.detailName.text = self.presentedRecipe.title;
self.detailImage.image = [UIImage imageNamed:self.presentedRecipe.imageName];
NSMutableString *ingredientText = [NSMutableString string];
for (NSString* ingredient in myRecipeDM.ingredients) {
[ingredientText appendFormat:#"%#\n", ingredient];
}
self.ingTV.text = (NSString*) self.presentedRecipe.ingredients;
}
Try
NSString *final=[[NSString alloc] init];
for (NSString* ingredient in myRecipeDM.ingredients)
{
if[ingredientText isEqualToString:""]
{
ingredientText=ingredient;
}
else
{
ingredientText=final;
ingredientText=[final stringByAppendingFormat:#"%#\n", ingredient];
}
}
You can use:
NSString *ingredientstring = #"";
for (NSString* ingredient in myRecipeDM.ingredients)
{
ingredientstring = [NSString stringWithFormat:#"%#\n%#", ingredientstring, ingredient];
}
_ingTV.text = ingredientstring;

Get all inside NSDictionary into a UITextView

I got a NSDictionary and I want to print it into a UITextView.
I tried using this code, but when I try to get it into a UITextView it gets only one line of text, when I NSLog it it works perfectly.
for(int i=0; i<[array count];i++) {
dictionary = [array objectAtIndex:i];
//that shows only one line from the dictionary in the textview
textview.text = [dictionary objectForKey:#"text"];
//that shows all the dictionary with key "text" in the log
NSLog(#"%#", [dictionary objectForKey:#"text"]);
}
is there a way to solve this problem?
you should append it to the existing text
textview.text = #"";
for(int i=0; i<[array count];i++)
{
dictionary= [array objectAtIndex:i];
//that shows only one line from the dictionary in the textview
textview.text = [textview.text stringByAppendingFormat:#"%#\n",[diction objectForKey:#"text"]]];
//that shows all the dictionary with key "text" in the log
NSLog(#"%#",[diction objectForKey:#"text"]);
}
The problem is that textview.text is overwriting the previous contents of textview.text.
What you need to do is append the string to the existing value of textview:
textview.text = [textview.text stringByAppendingString:[diction objectForKey:#"text"]];
But this will get all the values side by side, so you may want to add a space in between the values:
NSString *appendedText = [NSString stringWithFormat:#"%# %#", textview.text, [diction objectForKey:#"text"]];
textview.text = appendedText;
Finally, you can use modern Objective-C literals to access your dictionary objects like array[i] and diction[#"text"] instead of using objectAtIndex and objectForKey to type a little less.

select indexOfObject from NSArray in Xcode by comparing string

Hi I have NSarray values in Xcode. I need to get array indexOfObject by comparing string values.
my array values are
(
{
firstName = lord;
lastname = krishna;
},
{
firstName = priya;
lastname = amirtha;
}
)
If I type first name in textfield and click button means last name want to display in another textfield.
thank you.
To answer the title of your question:
NSString *compareString = #"something";
NSMutableArray *indexesOfMatches = [[NSMutableArray alloc]init];
for (NSString *string in theArray) {
if ([string isEqualToString:compareString]) {
NSNumber *index = [[NSNumber numberWithInterger:[theArray indexOfObject:string]];
[indexOfMatches addObject:index];
}
}
//indexOfMatches will now contain NSNumber objects that represent the indexes of each of the matching string objects in the array
I think that using an NSDictionary would be better for you though. Then you can simply keep the first and last names as Key Value pairs.
NSDictionary *names = #{#"lord" : #"krishna", #"priya" : #"amirtha" };
Then you can just do value for key when you get the first name:
NSString *firstName = #"lord";
NSString *lastName = [names valueForKey:firstName];
Store firstNameArray and lastNameArray a mutable array NSMutableArray.
Using Fast Enumeration. Suppose array is the array you are provided with
for (NSDictionary *item in array) {
[firstNameArray addObject:[item objectForKey:#"firstName"]];
[lastNameArray addObject:[item objectForKey:#"lastName"]];
}
After entering the data in firstNameTextField click the button
Button action method implementation
-(IBAction)btnClicked:(id)sender {
NSInteger index = [firstName indexOfObject:[firstNameTextField text]];
[lastNameTextField setText:[lastName objectAtIndex:index]];
}

UITableView cell subtitles from plist data

I want to add some subtitles to my UITableView cells (where I display continents, countries, other data). Already populated my first table with continents. Now I want to add numbers of countries as subtitle in every continent cell. I added:
int numberEurope;
numberEurope = [europe count]; //this works fine
int numberAfrica;
numberAfrica = [africa count]; //this works fine
NSNumber *myNum1 = [NSNumber numberWithInt:numberEurope];
NSNumber *myNum2 = [NSNumber numberWithInt:numberAfrica];
NSArray *myArray = [NSArray arrayWithObjects: myNum1, myNum2, nil];
cell.textLabel.text = ContinentName;
cell.detailTextLabel.text = [[NSString alloc] initWithFormat:#"%# countries", myArray];
return cell;
But the subtitles are the same in every cell, the full array is shown: ( 2, 2) countries instead of 2 countries in the first cell and 2 countries in the second one. What am I doing wrong? The plist I use is screenshot here: Cannot Feed UITableView with .plist
You have to select proper value based on indexPath:
cell.detailTextLabel.text = [[NSString alloc] initWithFormat:#"%d countries", [[myArray objectAtIndex:[indexPath row]] intValue]];

Table Views with plists

I have a plist with the structure:
Root - Dictionary
Notes - Array
Item 0 - Dictionary
Title - String
Text - String
Date - String
I am then doing:
...
NSString *noteTitle;
NSString *noteText;
NSString *noteDate;
self.notes = [self.data objectForKey:#"Notes"];
And configure the cell like this:
cell.textLabel.text = [[self.notes objectAtIndex:indexPath.row] objectForKey:#"Title"];
cell.detailTextLabel.text = [[self.notes objectAtIndex:indexPath.row] objectForKey:#"Date"];
How do I then, in a button, add the noteTitle to the "Title" value in the plist?
[self.notes addObject:noteTitle];
When the alert is shown you need to know the index of the table you're editing so you know what index to edit when the button is tapped (row below). Then, you'll be doing something like:
[[self.notes objectAtIndex:row] setObject:noteTitle forKey:#"Title"];
If you really want to add a whole new item then you should be creating a new mutable dictionary, adding noteTitle to it and then adding that dictionary to self.notes (in which case you don't need the row).
NSDictionary *d = [[NSDictionary alloc] initWithObjectsAndKeys:noteTitle, #"Title",noteText, #"Text",noteDate, #"Date", nil];
[self.notes addObject:d];
if you want to fold 2 strings:
NSString *newString=[NSString stringWithFormat:#"%#%#",oneString,secondString];

Resources