I have 3 NSArrays, each one with 6 objects:
NSArray *A [Joe, John, Jay, Jason, Jonah, Jeremiah];
NSArray *B [Doe, Smith, Scott, Jackson, Johnson, Lewis];
NSArray *C [1,2,3,4,5,6];
My model is:
#interface Person : NSObject
#property NSString *firstName;
#property NSString *lastName;
#property NSString *number;
#end
I need to create a forth array where each person object has a firstName, lastName, number.
NSArray *D = [0]Joe, Doe, 1
[1]John, Smith, 2
[2]Jay.Scott,3
[3]Jason, Jackson, 4
[4]Jonah, Johnson, 5
[5]Jeremiah. Lewis, 6
How can I do this?
You can do something like following: (On a side note, please declare your class property with proper attributes)
NSArray *A = #[#"Joe", #"John", #"Jay", #"Jason", #"Jonah", #"Jeremiah"];
NSArray *B = #[#"Doe", #"Smith", #"Scott", #"Jackson", #"Johnson", #"Lewis"];
NSArray *C = #[#1, #2, #3, #4, #5, #6];
NSMutableArray *D = [[NSMutableArray alloc] initWithCapacity:A.count];
for (int i=0; i < A.count; i++)
{
Person *p = [[Person alloc] init];
p.firstName = [A objectAtIndex:i];
p.lastName = [B objectAtIndex:i];
p.number = [C objectAtIndex:i];
[D addObject:d];
}
Let me know, how it goes.
Try using enumerateObjectsUsingBlock for array :-
NSArray *A = #[#"Joe", #"John", #"Jay", #"Jason", #"Jonah", #"Jeremiah"];
NSArray *B = #[#"Doe", #"Smith", #"Scott", #"Jackson", #"Johnson", #"Lewis"];
NSArray *C = #[#1, #2, #3, #4, #5, #6];
NSMutableArray *mutArr=[NSMutableArray array];
[A enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
Person *p=[[Person alloc]init];
p.firstName=A[idx];
p.lastName=B[idx];
p.number=C[idx];
[mutArr addObject:p];
}];
NSLog(#"person=%#",mutArr);
Related
I have an NSString filled with objects seperated by a comma
NSString *string = #"1,2,3,4";
I need to seperate those numbers and store then into an array while editing them, the result should be
element 0 = 0:1,
element 1 = 1:2,
element 2 = 2:3,
element 3 = 3:4.
How can i add those to my objects in the string ??
Thanks.
P.S : EDIT
I already did that :
NSString *string = #"1,2,3,4";
NSArray *array = [string componentsSeparatedByString:#","];
[array objectAtIndex:0];//1
[array objectAtIndex:1];//2
[array objectAtIndex:2];//3
[array objectAtIndex:3];//4
I need the result to be :
[array objectAtIndex:0];//0:1
[array objectAtIndex:1];//1:2
[array objectAtIndex:2];//2:3
[array objectAtIndex:3];//3:4
In lieu of a built in map function (yey for Swift) you would have to iterate over the array and construct a new array containing the desired strings:
NSString *string = #"1,2,3,4";
NSArray *array = [string componentsSeparatedByString:#","];
NSMutableArray *newArray = [NSMutableArray arrayWithCapacity:array.count];
[array enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
[newArray addObject:[NSString stringWithFormat:#"%lu:%#", (unsigned long)idx, obj]];
}];
The first thing you need to do is separate the string into an array of component parts - NSString has a handy method for that : '-componentsSeparatedByString'. Code should be something like this :
NSArray *components = [string componentsSeparatedByString:#","];
So that gives you 4 NSString objects in your array. You could then iterate through them to make compound objects in your array, though you arent exactly clear how or why you need those. Maybe something like this :
NSMutableArray *resultItems = [NSMutableArray array];
for (NSString *item in components)
{
NSString *newItem = [NSString stringWithFormat:#"%#: ... create your new item", item];
[resultItems addObject:newItem];
}
How about this?
NSString *string = #"1,2,3,4";
NSArray *myOldarray = [string componentsSeparatedByString:#","];
NSMutableArray *myNewArray = [[NSMutableArray alloc] init];
for (int i=0;i<myOldarray.count;i++) {
[myNewArray addObject:[NSString stringWithFormat:#"%#:%d", [myOldarray objectAtIndex:i], ([[myOldarray objectAtIndex:i] intValue]+1)]];
}
// now you have myNewArray what you want.
This is with consideration that in array you want number:number+1
i'm trying to filer an array of this Object:
MyObject.h
#interface MyObject : NSObject
#property (assign) int id_object;
#property (nonatomic, strong) NSString *name;
#end
So for example in my firstArray i have some MyObject:
MyObject *ob1 = [[MyObject alloc] init];
[ob1 setId_Object:1;
[ob1 setName:#"Carl"];
MyObject *ob2 = [[MyObject alloc] init];
[ob2 setId_Object:2;
[ob2 setName:#"Carl"];
MyObject *ob3 = [[MyObject alloc] init];
[ob3 setId_Object:3;
[ob3 setName:#"Carl"];
NSArray *firstArray = [[NSArray alloc] initWithObjects:ob1,ob2,ob3,nil];
then i want filter this array with NSPredicate with an array of NSNumber, like this:
NSArray *secondArray = [NSArray arrayWithObjects:[NSNumber numberWithInt:1],[NSNumber numberWithInt:2],nil];
i have tried this:
NSArray *filteredObject = [firstArray filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:#"id_object IN %#",secondArray]];
but the filteredObject is empty, how i can do?
Seems to be your test case that is wrong. I don't think your setId_object does what you think it does when calling it with an NSNumber;
[ob1 setId_object:[NSNumber numberWithInt:1]];
NSLog(#"%d", ob1.id_object);
> 391
[ob1 setId_object:1];
NSLog(#"%d", ob1.id_object);
> 1
Replacing the initializations makes your test case run ok.
EDIT: My test case, cut'n'pasted;
MyObject *ob1 = [[MyObject alloc] init]; ob1.id_object = 1; [ob1 setName:#"Carl"];
MyObject *ob2 = [[MyObject alloc] init]; ob2.id_object = 2; [ob2 setName:#"Carl"];
MyObject *ob3 = [[MyObject alloc] init]; ob3.id_object = 3; [ob3 setName:#"Carl"];
NSArray *firstArray = [[NSArray alloc] initWithObjects:ob1,ob2,ob3,nil];
NSArray *secondArray = [NSArray arrayWithObjects:[NSNumber numberWithInt:1],[NSNumber numberWithInt:2],nil];
NSArray *filteredObject = [firstArray filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:#"id_object IN %#",secondArray]];
NSLog(#"%#", filteredObject);
>>> <MyObject: 0x100110350> {1,"Carl"}
>>> <MyObject: 0x1001104b0> {2,"Carl"}
once check this one,
NSArray *filteredObject = [questionSections filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:#"self.Carl contains %# OR self.Carl contains %# ",#"1",#"2"]];
EX:-
NSArray* questionSections=[[NSArray alloc] initWithObjects:[[NSDictionary alloc] initWithObjectsAndKeys:
#"List 3 rooms in your house?",#"name",#"301q.png",#"questionpicture",nil],[[NSDictionary alloc] initWithObjectsAndKeys:#"Name a commonbird?",#"name",#"202q.png",#"questionpicture",nil], nil];
NSArray *filteredObject = [questionSections filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:#"self.questionpicture contains %# OR self.questionpicture contains %# ",#"2",#"3"]];
NSLog(#"%#",filteredObject);
O/P:--
(
{
name = "List 3 rooms in your house?";
questionpicture = "301q.png";
},
{
name = "Name a common bird?";
questionpicture = "202q.png";
}
)
try replace
#property (nonatomic, copy) NSNumber* id_object;
#property (nonatomic, copy) NSString *name;
instead of
#property (assign) int id_object;
#property (nonatomic, strong) NSString *name;
I have a NSArray of Contact objects, we can call it contacts. Contact is a superclass, FacebookGroup and Individual are subclasses of Contact. FacebookGroup has a property called individuals which is a set of Individual objects.
I also have a NSArray of NSString objects, we can call it userIDs.
What I want to do is create a new NSArray from the existing contacts array that match the userIDs in userIDs.
So if contacts has 3 Contact objects with userID 1,2 and 3. And my userIDs has a NSString object 3. Then I want the resulting array to contain Contact which equals userID 3.
Contact.h
Contact : NSObject
FacebookGroup.h
FacebookGroup : Contact
#property (nonatomic, strong) NSSet *individuals;
Individual.h
Individual : Contact
#property (nonatomic, strong) NSString *userID;
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"userId = %#", myContact.userId];
NSArray *filteredArray = [contacts filteredArrayUsingPredicate:predicate];
Is this what you are looking for?
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"userID IN %#", userIDs];
NSArray *filtered = [contacts filteredArrayUsingPredicate:predicate];
i'm expecting you want like this once see this one,
NSMutableArray *names = [NSMutableArray arrayWithObjects:#"one", #"two", #"three", #"four", nil];
NSMutableArray *ids = [NSMutableArray arrayWithObjects:#"1", #"2", #"2", #"3", nil];
NSMutableArray *array=[[NSMutableArray alloc]init];
for(int i=0;i<[ids count];i++){
if([[ids objectAtIndex:i] isEqualToString:#"2"])
[array addObject:[names objectAtIndex:i]];
}
NSLog(#"%#",array);
O/P:-
(
two,
three
)
I have UISearchBar, UITableView, a web service which returns a NSMutableArray that contain objects like this:
//Food.h
Food : NSObject {
NSString *foodName;
int idFood;
}
#property (nonatomic, strong) NSString *foodName;
And the array:
Food *food1 = [Food alloc]initWithName:#"samsar" andId:#"1"];
Food *food2 = [Food alloc] initWithName:#"rusaramar" andId:#"2"];
NSSarray *array = [NSArray arrayWithObjects:food1, food2, nil];
How do I filter my array with objects with name beginning with "sa"?
You can filter any array like you'd like to with the following code:
NSMutableArray *array = ...;
[array filterUsingPredicate:[NSPredicate predicateWithBlock:^BOOL(id evaluatedObject, NSDictionary *bindings) {
return [evaluatedObject.foodName hasPrefix:searchBar.text];
}];
This will filter the array "in-place" and is only accessible on an NSMutableArray. If you'd like to get a new array that's been filtered for you, use the filteredArrayUsingPredicate: NSArray method.
NSString *predString = [NSString stringWithFormat:#"(foodName BEGINSWITH[cd] '%#')", #"sa"];
NSPredicate *pred = [NSPredicate predicateWithFormat:predString];
NSArray *array = [arr filteredArrayUsingPredicate:pred];
NSLog(#"%#", array);
I am trying to figure out how to implement this in Objective-C.
I want to remove the strings in an NSArray that have appear more than once in the array.
At the end I want to have an array that only has the unique lines in an array (meaning that not just the duplicates are deleted but also the original string that matches the duplicates.)
For example if you had the following array:
NSArray *array = [NSArray arrayWithObjects:#"bob", #"frank", #"sarah", #"sarah", #"fred", #"corey", #"corey", nil];
I would want the new array to look like this:
#"bob", #"frank", #"fred"
Use an NSCountedSet:
NSCountedSet *countedSet = [NSCountedSet setWithArray:yourArray];
NSMutableArray *finalArray = [NSMutableArray arrayWithCapacity:[yourArray count]];
for(id obj in countedSet) {
if([countedSet countForObject:obj] == 1) {
[finalArray addObject:obj];
}
}
#Caleb suggested adding a method to NSCountedSet called -objectsWithCount:,, which I've implemented here:
#interface NSCountedSet (JRCountedSetAdditions)
- (NSArray *) objectsWithCount:(NSUInteger) count;
#end
#implementation NSCountedSet (JRCountedSetAdditions)
- (NSArray *) objectsWithCount:(NSUInteger) count {
NSMutableArray *array = [NSMutableArray array];
for(id obj in self) {
if([self countForObject:obj] == count) {
[array addObject:obj];
}
}
return [array copy];
}
#end
Once that's done, all you need is one line:
NSArray *finalArray = [[NSCountedSet setWithArray:yourArray] objectsWithCount:1];
By the way, this is type-agnostic, so this will work with any Objective-C object. :-)
One liner : uniqueArray = [[NSSet setWithArray:duplicateArray] allObjects]; if you don't care about the ordering :D
A slightly different approach from Jacob's:
NSArray *array = [NSArray arrayWithObjects:#"bob", #"frank", #"sarah", #"sarah", #"fred", #"corey", #"corey", nil];
NSCountedSet *namesSet = [[NSCountedSet alloc] initWithArray:array];
NSMutableArray *namesArray = [[NSMutableArray alloc] initWithCapacity:[array count]];
[namesSet enumerateObjectsUsingBlock:^(id obj, BOOL *stop){
if ([namesSet countForObject:obj] == 1) {
[namesArray addObject:obj];
}
}];
And
NSLog(#"old: %#\nNew: %#", array, namesArray);
gives:
2011-06-16 18:10:32.783 SetTest[1756:903] old: (
bob,
frank,
sarah,
sarah,
fred,
corey,
corey
)
New: (
frank,
fred,
bob
)
Blocks are your friends! And since NSCountedSet is a subclass of NSSet you can use the block methods that are available there.
Here is the simplest approach to remove duplicate strings:
NSArray *array = [NSArray arrayWithObjects:#"bob", #"frank", #"sarah", #"sarah", #"fred", #"corey", #"corey", nil];
NSArray *distintStrings = [array valueForKeyPath:#"#distinctUnionOfObjects.self"];