how can i ask an array if it contains an item and if it does it to [[NSArray alloc] initWithObjects:#"those objects" automatically.
this is my fav .h
#interface FavoriteViewController : UITableViewController {
NSMutableArray *favoritesArray;
NSMutableArray *didContain;
}
#property (nonatomic, retain) NSMutableArray *favoritesArray;
#property (nonatomic, retain) NSMutableArray *didContain;
this is the .m
favoritesArray = [[NSMutableArray alloc]init];
didContain = [[NSMutableArray alloc]init];
if
([favoritesArray containsObject:#"one"])
{
[didContain addObject:#"one"];
}
and in the detail view controller.m i have this code
[[NSMutableArray alloc] init];
[favoritesArray addObject: #"one"];
i get the table to work however nothing shows up....
NSArray *yourArray = [[NSArray alloc] initWithObjects:#"Hello", #"World", nil];
NSMutableArray *didContain = [[NSArray alloc] init];
if([yourArray contains: #"Hello"])
{
[didContain addObject:#"Hello"];
}
or
NSArray *yourArray = [[NSArray alloc] initWithObjects:#"Hello", #"World", nil];
NSMutableArray *didContain = [[NSArray alloc] init];
[didContain addObject: [yourArray objectAtIndex:[yourArray indexOfObject:#"Hello"]];
All of this and more is readily available in the apple docs. Please do some google searching first next time. Good luck hope this helps.
Use filteredArrayUsingPredicate:
See NSArray Class Reference and Predicate Programming Guide
It appears that you are trying to use an uninitialized property in your detail view controller.
Normally, you initialize properties in your init: or viewDidLoad method implementations then before presenting the view in your parent view controller, set the property using the property accessors
This line:
// DetailViewController.m initializer code
[[NSMutableArray alloc] init]; // returned object is not used
Should be:
favoritesArray = [[NSMutableArray alloc] init]; // view controller initialization code
Instead of calling this:
[favoritesArray addObject:#"one"];
After you create your detailViewController set the favoritesArray with the filtered array
// FavoriteViewController.m
MyDetailViewController *dvc = [[MyDetailViewController alloc] initWithNibName:#"MyDetailViewController" bundle:nil];
// populate the array
[dvc setFavoritesArray:didContain];
// Assuming you are using a navigation controller
[navigationController pushViewController:dvc animated:YES];
[dvc release];
Related
I want to build a shared object that all the classes will be able to access to it.
i want that in this object will be NSMutableArray .
this is how i call this object
+(id)sharedManager{
#synchronized(self) {
if (sharedManager == nil){
sharedManager = [[self alloc] init];
array = [[NSMutableArray alloc] init];
}
}
return sharedManager;
}
and this is how i define the NSMutableArray :
#property (nonatomic,retain) NSMutableArray *array;
the problem is that after i create this NSMutableArray in the sharedManager method, every time i try to access the array is equal to Nil.
You're attempting to set an instance variable from a class method. Instead, you should create array in your -init method in the singleton. That way when you message, sharedManager = [[self alloc] init];, the array will be configured for that shared instance.
- (id)init
{
self = [super init];
if (!self)
return nil;
array = [[NSMutableArray alloc] init];
return self;
}
I am trying to build a Grouped Sectioned View. I want to set an array as an object in the dictionary but I came across the empty array Issues.
Is this Even possible????? Is There a trick ????
#import "RootViewController.h"
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:YES];
// Get the specific Node for this row.
Nodes *nodes = (Nodes *)[nodesMArray objectAtIndex:indexPath.row];
// Create the Details View Controller and initialize it.
nodesDetailView *viewController = [[nodesDetailView alloc] initWithStyle:UITableViewStyleGrouped ];
[[self navigationController] pushViewController:viewController animated:YES];
// Set the title of the view to the nodes name
viewController.title = [nodes computer_name];
//Information
//Network Data
viewController.ipaddress= [nodes ipaddress];
viewController.subnet_mask= [nodes subnet_mask];
viewController.gateway= [nodes gateway];
viewController.domain_name= [nodes domain_name];
}
#import "nodesDetailViewController. h
#interface nodesDetailView : UITableViewController{
//Network Data
NSString *ipaddress;
NSString *subnet_mask;
NSString *gateway;
NSString *domain_name;
//Grouped
NSMutableArray *Keys;
NSMutableDictionary *Contents;
}
#property (nonatomic, retain) NSMutableArray *Keys;
#property (nonatomic, retain) NSMutableDictionary *Contents;
#property (nonatomic, retain) NSString *ipaddress;
#property (nonatomic, retain) NSString *subnet_mask;
#property (nonatomic, retain) NSString *gateway;
#property (nonatomic, retain) NSString *domain_name;
#end
#import "nodesDetailViewController. m
#synthesize .......
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
NSMutableArray *keys = [[NSMutableArray alloc] init];
NSMutableDictionary *contents = [[NSMutableDictionary alloc] init];
NSString *network = #"Network Data";
NSString *product = #"Product Details";
//IS THIS POSSIBLE ??????? Because I get An Empty array
[contents setObject:[NSArray arrayWithObjects: ipaddress,subnet_mask, gateway, domain_name, nil] forKey:network];
return self;
}
Thank You In Advance.
All of those strings that you are adding to the array haven't even been allocated/initialized yet. That's why the array is empty. You can't just go: NSString *emptyString; and then go: [myArray addObject:emptyString];. Your strings are all empty.
Check the values u are passing from the table view. They maybe nil. I just used this much of data and i m getting correct NSLog.
NSMutableDictionary *contents = [[NSMutableDictionary alloc] init];
NSString *my = #"kk";
NSString *my1 = #"polko";
[contents setObject:[NSArray arrayWithObjects: my1,my, nil] forKey:#"o"];
NSLog(#"%#" , contents);
For Those Who are wondering: Problem Solved My code was fine. the problem was :
it was in the wrong place.
The Code Supposed to be in - (void)viewDidLoad and not in - (id)initWithStyle:
I want to thank everybody who took the time to look in to it.
I have no idea why i put the code there to begin with.
Thanx Again
I have a database app i am making and i can extract the results from a sqlite database and put them in the table, however i need to make them sort alphabetically in sections.
So now i have this code
AArray = [[NSMutableArray alloc] init];
BArray = [[NSMutableArray alloc] init];
CArray = [[NSMutableArray alloc] init];
DArray = [[NSMutableArray alloc] init];
EArray = [[NSMutableArray alloc] init];
FArray = [[NSMutableArray alloc] init];
GArray = [[NSMutableArray alloc] init];
HArray = [[NSMutableArray alloc] init];
IArray = [[NSMutableArray alloc] init];
JArray = [[NSMutableArray alloc] init];
KArray = [[NSMutableArray alloc] init];
LArray = [[NSMutableArray alloc] init];
MArray = [[NSMutableArray alloc] init];
NArray = [[NSMutableArray alloc] init];
OArray = [[NSMutableArray alloc] init];
PArray = [[NSMutableArray alloc] init];
QArray = [[NSMutableArray alloc] init];
RArray = [[NSMutableArray alloc] init];
SArray = [[NSMutableArray alloc] init];
TArray = [[NSMutableArray alloc] init];
UArray = [[NSMutableArray alloc] init];
VArray = [[NSMutableArray alloc] init];
WArray = [[NSMutableArray alloc] init];
XArray = [[NSMutableArray alloc] init];
YArray = [[NSMutableArray alloc] init];
ZArray = [[NSMutableArray alloc] init];
and i have code to move each item from the database into the relevant array, this all works fine.
I then have this code:
All = [NSMutableDictionary dictionaryWithObjectsAndKeys:AArray,#"A",BArray,#"B",CArray,#"C",DArray,#"D",EArray,#"E",FArray,#"F",GArray,#"G",HArray,#"H",IArray,#"I",JArray,#"J",KArray,#"K",LArray,#"L",MArray,#"M",NArray,#"N",OArray,#"O",PArray,#"P",QArray,#"Q",RArray,#"R",SArray,#"S",TArray,#"T",UArray,#"U",VArray,#"V",WArray,#"W",XArray,#"X",YArray,#"Y",ZArray,#"Z",nil];
AllArray = [NSArray alloc];
AllArray = [AllArray initWithArray:[[All allKeys]sortedArrayUsingSelector:#selector(compare:)]];
and this
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
// Return the number of sections.
return [AllArray count];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
// Return the number of rows in the section.
NSArray *Array = [All objectForKey:[AllArray objectAtIndex:section]];
return [Array count];
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
int sectionindex = section;
return [AllArray objectAtIndex:sectionindex];
}
When i run it works fine, however if i scroll up and down a few times the app crashes without any error message.
It is something to do with the sections as it crashes when i add them in, but i just cannot see what im doing wrong
Everythings declared in the h file and i #property and #synthesize the following
#property (nonatomic,retain) NSMutableDictionary *All;
#property (nonatomic,retain) NSArray *AllArray;
#property (nonatomic, retain) UITableView *TableView;
If anyone could help me it would be really great!
Thanks!
Your dictionary "All" does not seem to be retained. You appear to assign the dictionary object directly to the instance variable, not to the property (in which case you would have used self.All).
If your app crashes without a message, then make sure to enable the Breakpoints button in Xcode's toolbar. This will run the app in the debugger, which will give you more helpful information about the crash. Setting NSZombieEnabled to YES also helps.
Take a look here:
Documentation
The simplest approach is, to provide a sort selector (see the link for details):
sortedArray = [anArray sortedArrayUsingSelector:#selector(localizedCaseInsensitiveCompare:)];
I thought I knew how to deal with memory leaks and arrays, but then this pops up. I can't figure pout why this is leaking:
// MyViewController.h
NSMutableArray *myMutableArray;
#property (nonatomic, retain) NSMutableArray *myMutableArray;
// MyViewController.m
#synthesize myMutableArray;
- (void) viewDidLoad {
if (self.myMutableArray == nil) {
self.myMutableArray = [[NSMutableArray alloc] init];
}
. . .
for (NSUInteger i = 0; i < someCount; ++i) {
[self.myMutableArray addObject:[NSString stringWithFormat: #"%#",myString]];
}
}
- (void)viewDidUnload {
self.myMutableArray = nil
}
- (void)dealloc {
[myMutableArray release];
}
Your problem is here:
if (self.myMutableArray == nil) {
self.myMutableArray = [[NSMutableArray alloc] init];
}
It should be:
if (myMutableArray == nil) {
self.myMutableArray = [[[NSMutableArray alloc] init] autorelease];
}
Or:
if (myMutableArray == nil) {
myMutableArray = [[NSMutableArray alloc] init];
}
Explanation:
Since you are using retain as a property mutator attribute, the object will be retained when it is passed to the property setter, therefore you have a leak when you retain an object you already have ownership of.
The solution to this is to either a) Pass an autorelease-d object to the property setter or b) Assign the ivar directly to the alloc-ed object.
You're allocating a new array, then setting it to a retain property. Change that line to
self.myMutableArray = [NSMutableArray array];
self.myMutableArray = [[NSMutableArray alloc] init];
should be
self.myMutableArray = [[[NSMutableArray alloc] init] autorelease];
because myMutableArray is a retained property.
I have a UITabBarController that is loading a UINavigationController.
When I push a new view controller, is it possible to change the UITabBarController to a new set of tabs?
Sure, why not just re-assign your new UIViewController's array to UITabBarController's viewControllers property? That will do the trick.
- (void)addNewControllers {
NSMutableArray *controllers = [NSMutableArray array];
UIViewController *c0 = [[[UIViewController alloc] init] autorelease];
[controllers addObject: c0];
UIViewController *c1 = [[[UIViewController alloc] init] autorelease];
[controllers addObject: c1];
UIViewController *c2 = [[[UIViewController alloc] init] autorelease];
[controllers addObject: c2];
[myTabBarController setViewControllers:controllers];
}
Read more about the potential side effects here.