i have an array like
[chapter,indent,left,indent,nonindent,chapter,chapter,indent,indent,left];
i need to find indexes of duplicates and also non duplicate elements .
how to do this...........give some sample code or logic......
thanks in advance
iam using objective c.....
NSArray *myWords = [string componentsSeparatedByString:#"class=\""];
int count_var=[myWords count];
tmp1=#"";
for(int i=1;i<count_var;i++)
{
str=[NSString stringWithFormat:#"\n%#",[myWords objectAtIndex:i]];
class=[str componentsSeparatedByString:#"\""];
NSString *tmp=[NSString stringWithFormat:#"%#",[class objectAtIndex:0]];
tmp1=[[NSString stringWithFormat:#"%#",tmp1] stringByAppendingString:[NSString stringWithFormat:#"%#",tmp]];
}
t1.editable=NO;
t1.text=tmp1;
NSArray *tempo=[[NSArray alloc]init];
tempo=[tmp1 componentsSeparatedByString:#"\n"];
tempCount=[tempo count];
this is my sample code...in this the array tempo contains all objects from that array i want to get index of duplicate strings≥.
You could build a dictionary mapping the objects to index sets. For every index set, a -count of 1 means no duplicates, > 1 means there are duplicates.
NSArray *arr = [NSArray arrayWithObjects:...];
NSMutableDictionary *dict = [NSMutableDictionary dictionary];
for (NSUInteger i=0; i<[arr count]; ++i) {
id obj = [arr objectAtIndex:i];
NSMutableIndexSet *ids = [dict objectForKey:obj];
if (!ids) {
ids = [NSMutableIndexSet indexSet];
[dict setObject:ids forKey:obj];
}
[ids addIndex:i];
}
NSLog(#"%#", dict);
Related
My Problem Scenario is like this. I have an NSMutableArray ( Every Object is Nsstring). I have a UItextField ( as Client said) for Search.
I want know how to Search String into NSMutableArray like this
if I type A into textfield only those Content come from NSMutableArray which start From A.
if I type AB into TextField only those Content Comes from NSMutableArray which is started from AB..
....
I am Trying NSRange Concept I like share Mycode
~
for (int i=0; i<[[localTotalArrayForAwailable objectForKey:#"PUNCH"] count]; i++)
{
NSString *drinkNamePuch= [[[localTotalArrayForAwailable objectForKey:#"PUNCH"] objectAtIndex:i] drinkNames];
NSRange titleResultsRange = [drinkNamePuch rangeOfString:searchText options:( NSCaseInsensitiveSearch)];
if (titleResultsRange.length>0)
{
[searchArraypuch addObject:[[localTotalArrayForAwailable objectForKey:#"PUNCH"] objectAtIndex:i]];
[copyListOfItems setValue:searchArraypuch forKey:#"PUNCH"];
}
}
~
Based on this code search not working proper as i need.
Thanks
If you're trying to find all of the strings that match your searchText from the beginning, then you should check:
if ( titleresultsRange.location == 0 )
Other than that, I am not sure what is "not working proper", you need to provide a better explanation of what your expected results are, and what your actual results are.
Do this;
NSPredicate* predicate = [NSPredicate predicateWithFormat:#"SELF BEGINSWITH[cd] %#", searchText];
NSArray* filteredStrings = [[localTotalArrayForAwailable objectForKey:#"PUNCH"] filteredArrayUsingPredicate:predicate];
In filteredStrings you got all the strings that begins with searchText.
You might find Predicate Programming Guide helpful.
try this logic....it is working
NSMutableArray *arr = [[NSMutableArray alloc]initWithObjects:#"aa",#"bbb",#"bb",#"cc",#"dd",#"ee",#"ff",#"gg",#"hh",#"ii", nil];
NSMutableArray *arrNew = [[NSMutableArray alloc]init];
NSString *strSearch = #"cccc";
int k = strSearch.length;
for (int i=0; i<[arr count]; i++) {
for (int j=0; j<k; j++) {
if (k<=[[arr objectAtIndex:i] length]) {
if ([strSearch characterAtIndex:j] != [[arr objectAtIndex:i]characterAtIndex:j]) {
break;
}
else if(j == k-1){
[arrNew addObject:[arr objectAtIndex:i]];
}
}
}
}
NSLog(#"%#",[arrNew description]);
You can use these methods, which are provided by NSArray/NSMutableArray:
In NSArray see section "Finding Objects in an Array" for filtering methods starting with "indexesOfObjects...", e.g. indexesOfObjectsPassingTest:
In NSArray see section "Deriving New Arrays" for the method filteredArrayUsingPredicate:
In NSMutableArray there is a method filterUsingPredicate:
For narrowing the results you can continue applying the filtering consecutively to the filtered arrays or index sets.
Example with indexesOfObjectsPassingTest: using a block:
NSArray *strings = [NSArray arrayWithObjects:#"A", #"a", #"aB", #"AbC", #"Bag", #"Babc", #"baCK", #"", #"dba", nil];
NSString *searchString = #"Ab";
BOOL (^startsWithPredicate)(id, NSUInteger, BOOL*) = ^BOOL (id obj, NSUInteger idx, BOOL *stop) {
NSString *string = (NSString *) obj;
NSRange range = [string rangeOfString:searchString options:NSCaseInsensitiveSearch];
return (range.location == 0);
};
NSIndexSet *indexSet = [strings indexesOfObjectsPassingTest:startsWithPredicate];
NSLog(#"Strings found: %#", [strings objectsAtIndexes:indexSet]);
Output:
Strings found: (
aB,
AbC
)
I'm new in iPhone, I want to add elements to NSMutableArray with each element's name
I created a MutableArray for keys , then other array for elements that I get them from object called Pages.
I wrote the following code
NSMutableArray *myArray;
NSMutableArray *arrayKey = [[NSMutableArray alloc] initWithObjects:#"b_pag_id", #"b_pag_bo_id", #"b_pag_num", #"b_pag_note", #"b_page_mark", #"b_page_stop", #"b_pag_user_id", nil];
for (int x=0; x<[pages count]; x++) {
Pages *myPages = (Pages *)[self.pages objectAtIndex:x];
NSString *b_pag_id2 = [NSString stringWithFormat:#"%d",myPages.b_pag_id];
NSString *b_pag_bo_id2 = [NSString stringWithFormat:#"%d",myPages.b_pag_bo_id];
NSString *b_pag_num2 = [NSString stringWithFormat:#"%d",myPages.b_pag_num];
NSString *b_pag_note2 = myPages.b_pag_note;
NSString *b_page_mark2 = [NSString stringWithFormat:#"%d",myPages.b_page_mark];
NSString *b_page_stop2 = [NSString stringWithFormat:#"%d",myPages.b_page_stop];
NSString *b_pag_user_id2 = [NSString stringWithFormat:#"%d",myPages.b_pag_user_id];
NSMutableArray *arrayValue = [[NSMutableArray alloc] initWithObjects:b_pag_id2, b_pag_bo_id2, b_pag_num2, b_pag_note2, b_page_mark2, b_page_stop2, b_pag_user_id2, nil];
NSDictionary *theReqDictionary = [NSDictionary dictionaryWithObjects:arrayValue forKeys:arrayKey];
myArray = [NSMutableArray arrayWithObjects:theReqDictionary,nil];
}
NSLog(#"array size: %d", [myArray count]);
I want to add every element to its key for example
element (b_pag_id2) its key (b_pag_id) ..etc
is this right ?? or how to do this ??
consider that NSLog(#"array size: %d", [myArray count]); gives me 1 and the size of my elements is 14
Before the loop you need to initialize the aray
NSMutableArray *myArray = [NSMutableArray array];
Inside the loop replace following:
myArray = [NSMutableArray arrayWithObjects:theReqDictionary,nil];
with
[myArray addObject:theReqDictionary];
The problem is that you are creating a new array with 1 dictionary in every loop iteration. Instead you need to initialize the array and add values one by one.
Each time through your loop you are creating a new array for myArray that has only one element. You should initialize an empty NSMutableArray before the loop and then simply add your new object to it instead of using arrayWithObjects: to create myArray..
Here i'm giving a short example, and i hope this will help you.
see this code :-
NSMutableArray *arrayValue = [[NSMutableArray alloc]initWithObjects:#"Value1",#"Value2",#"Value3", nil];
NSMutableArray *arrayKey = [[NSMutableArray alloc]initWithObjects:#"1",#"2",#"3", nil];
NSMutableDictionary *dic = [[NSMutableDictionary alloc]init];
for(int i=0;i<3;i++)
{
[dic setObject:[arrayValue objectAtIndex:i] forKey:[arrayKey objectAtIndex:i]];
}
//and you can see this by printing it using nslog-
NSLog(#"%#",[dic valueForKey:#"1"]);
Thank you!!!
I am getting this data from JSON web services
[{"identity":"DEMO","assets":[{"identity":"34DL3611","systemId":"544507"},{"identity":"34GF0512","systemId":"5290211"},{"identity":"34HH1734","systemId":"111463609"},{"identity":"34HH1736","systemId":"111463622"},{"identity":"34YCJ15","systemId":"294151155"}],"systemId":4921244}]
I am using this method to get the values of assets for identity ("assets":[{"identity":"34DL3611","systemId":"544507"}):
vehicleList = [dict objectForKey: #"assets"];
self.listVehicles = [[NSMutableArray alloc] init];
for (NSUInteger index = 0; index < [vehicleList count]; index++) {
itemDict = [vehicleList objectAtIndex: index];
[self.listVehicles addObject:[itemDict objectForKey:#"identity"]];
}
how can I get the systemId values ... ?
I have tried this for systemId
vehicleListID = [dict objectForKey:#"systemId"];
self.listVehiclesID =[[NSMutableArray alloc]init];
for (NSUInteger index = 0; index < [vehicleListID count]; index++) {
assetsIdDict = [vehicleListID objectAtIndex: index];
[self.listVehiclesID addObject:[assetsIdDict objectForKey:#"systemId"]];
}
but getting error: [__NSCFNumber count]: unrecognized selector sent to instance 0x784c730
You're basically almost there. You just want this probably:
NSString *systemId = [itemDict objectForKey:#"systemId"];
With regard to this code you have:
vehicleListID = [dict objectForKey:#"systemId"];
self.listVehiclesID =[[NSMutableArray alloc]init];
for (NSUInteger index = 0; index < [vehicleListID count]; index++) {
assetsIdDict = [vehicleListID objectAtIndex: index];
[self.listVehiclesID addObject:[assetsIdDict objectForKey:#"systemId"]];
}
... well that is just totally wrong. That's returning the NSNumber which in your example JSON is 4921244 right at the end of the JSON string. You're then calling count on it, but it's not an array, so it crashes.
If you want to get all the values out of that JSON you can use this:
NSNumber *outerIdentity = [dict objectForKey:#"identity"];
NSNumber *outerSystemId = [dict objectForKey:#"systemId"];
NSArray *vehicleList = [dict objectForKey:#"assets"];
for (NSUInteger index = 0; index < [vehicleList count]; index++) {
NSDictionary *itemDict = [vehicleList objectAtIndex: index];
NSString *identity = [itemDict objectForKey:#"identity"];
NSString *systemId = [itemDict objectForKey:#"systemId"];
}
Then do whatever it is you want to do with all those objects.
The json response you are getting is an array. vehicleList should be the objectatIndex 0 of json response.
NSDictionary *vehicleList = [[arrayLoadedFromJson objectAtIndex:0] objectForKey: #"assets"];
vehicleList = [dict objectForKey: #"assets"];
self.listVehicles = [[NSMutableArray alloc] init];
for (NSUInteger index = 0; index < [vehicleList count]; index++) {
itemDict = [vehicleList objectAtIndex: index];
[self.listVehicles addObject:[itemDict objectForKey:#"identity"]];
[self.listVehicles addObject:[itemDict objectForKey:#"systemId"]];
}
This you have to insert:-in your code
[self.listVehicles addObject:[itemDict objectForKey:#"systemId"]];
For that to having in separate array, you need to initialize new NSMutableArray and add the objects.
Else, the better way is, declare a new class with 2 NSString members say, identity and systemId. Extract the values from JSON and assign to the class members. And add that object to the array.
i have this code :
NSMutableArray *arr = [NSMutableArray array];
for (int i = 0; i <[titleNews count]; ++i) {
TFHppleElement *someText =[titleNews objectAtIndex:i];
NSString *result = [someText content];
[arr addObject:result];
if ([[arr objectAtIndex:i]isEqualToString:#"-- : --"]) {
[arr replaceObjectAtIndex:i withObject:#"x"];
[arr addObject:#"/"];
}
NSLog(#"%#",[arr objectAtIndex:i]);
}
the (replaceObjectAtIndex: withObject:) work fine but the (addObject)method not working
You're presuming that the "result" that you add to the array will always be at index i. But the first time that you fall into your "if" body, you'll add an i+1 element ("/") and from then on everything will be off by one.
I am getting the error:
*** -[NSArray subarrayWithRange:]: index 9779 beyond bounds [0 .. 9776]'
***
and I am not sure how to fix it.
If you could tell me that would be great!
NSArray *keys = [NSArray arrayWithObjects:#"type", #"name", #"street", #"address1", #"address2", #"town", #"county", #"postcode", #"number", #"coffee club", #"latitude", #"longitude", nil];
for (int i = 0; i < [chunks count]; i += [keys count])
{
NSArray *subarray = [chunks subarrayWithRange:NSMakeRange(i, [keys count])];
NSDictionary *dict = [[NSDictionary alloc] initWithObjects:subarray forKeys:keys];
NSLog(#"%#", dict);
// do something with dict
[dict release];
}
You don't say what chunks is in your code snippet.
I guess the error is that you are accessing over the bounds of the chunks array.
Maybe something like this would work better:
for (int i = 0; i + [keys count] <= [chunks count]; i += [keys count])
To elaborate a bit more. You are taking a sub array which starts at i and goes to [keys count] more elements, but there is no check that i + [keys count] doesn't go over the chunk array size. Perhaps that's causing a problem?
NSMakeRange does not work this way.
The forst parameter is the starting index and the second is the length of the subarray.
In your case, your code should look like this:
NSArray *subarray = [chunks subarrayWithRange:NSMakeRange(i, [keys count] - i)];
This is why you were getting the "index out of bounds" exception.