How to get the desired dictionary result using mutable arrays in ios - ios

I have three mutable array:
wkdatearray values like this: date from 2016-01-10 to 2016-01-16.
spentonarray values like this: 2016-01-10 to 2016-01-13.
hoursarray values like this: 7,3,4,5,1.
So, I compare the index objects of wkdatearray and spentarray like this:
for (int i = 0; i < self.wkDateArray.count; i++) {
for (int j = 0; j < self.spentonArray.count; j++) {
if ([
[self.wkDateArray objectAtIndex: i] isEqualToString: [self.spentonArray objectAtIndex: j]
]) {
NSLog(# "Matched Indexes %d %#", i, [self.wkDateArray objectAtIndex: i]);
} else {
}
}
Now I want to get the result like :
If the index value of self.wkdatearray and self.spentonarray are matched or equals then I have to set the hours array value as object for self.wkdatearray[0],[1]…[6]. like this in newsheet dictionary. Now I manually put string #"".
newSheet = [NSDictionarydictionaryWithObjectsAndKeys:strEntryID,entryID, proj,project,projID,projectId, strIssue,issue,strIssueID,issueId, strActivity,activity,strActivityId,activityId,#"",comment,#"",self.wkDateArray[0],#"",self.wkDateArray[1],#"",self.wkDateArray[2],#"",self.wkDateArray[3],#"",self.wkDateArray[4],#"",self.wkDateArray[5],#"",self.wkDateArray[6], nil];
If the index value of self.wkdatearray and self.spentonarray are not matched and not equals then the hours value like this #“‘ as object for self.wkdatearray[0].like this in newsheet dictionary. Now, I put by default object values as #""for self.wkdatearray[0...6].
Here the hours array having 5 values only.
How to do both the conditions inside the for loop? Or is there any other way to do this?

First of all make the newSheet dictionary mutable. You do not need 2 for loops.
int minIndexCount = MIN(self.wkdatearray.count, self. spentonArray.count)
we first take the min of wkdatearray.count and spentonArray.count since for example if there are only 2 items in spentonArray then if we try to use index 4 of wkdatearray then we do not have index 4 in spentonArray, meaning the primary condition that the indexes of both wkdatearray and spentonArray should be equal fails. Hence by taking min we ensure that we are only going to compare those indexes whixha re available in both wkdatearray and spentonArray.
for (int i = 0; i < minIndexCount; i++) {
if ([[self.wkDateArray objectAtIndex: i] isEqualToString: [self.spentonArray objectAtIndex: i]])
{
NSLog(# "Matched Indexes %d %#", i, [self.wkDateArray objectAt Index: i]);
[newSheet setValue:self.hoursarray[i] forKey:self.weekArray[i]]
} else {
}
}

If by "matched or equals" you mean that if the index's are the same AND the strings are matched.
for (int i = 0; i < self.wkDateArray.count; i++) {
for (int j = 0; j < self.spentonArray.count; j++) {
if (i==j && [
[self.wkDateArray objectAtIndex: i] isEqualToString: [self.spentonArray objectAtIndex: j]
]) {
NSLog(# "Matched Indexes %d %#", i, [self.wkDateArray objectAtIndex: i]);
} else {
}
}
Take note of the beginning of the if statement that has if (i==j && ...

Related

Regex Password Validation using Or

I need to validate a password with these rules:
Password must have 6-12 characters
and at least two of the following:
Uppercase letters, Lowercase letters, Numbers or Symbols.
Below is my current regex:
^((?=.*[a-z])|(?=.*[A-Z])|(?=.*\\d)|(?=.*(_|[-+_!##$%^&*.,?]))).{6,12}
I am struggling about how to make the 'at least' condition.
You may define a function to check your requirements one by one and increment a counter to see how many of them actually are met. If more than 1 matched and the string length is between 6 and 12, the password passes.
NSUInteger checkPassword(NSString * haystack) {
NSArray * arr = [NSArray arrayWithObjects: #"(?s).*\\d.*", #"(?s).*[a-z].*", #"(?s).*[A-Z].*", #"(?s).*[-+_!##$%^&*.,?].*",nil];
NSUInteger cnt = 0;
for (NSUInteger index = 0; index < [arr count]; ++index) {
NSPredicate * passTest = [NSPredicate predicateWithFormat:#"SELF MATCHES %#", [arr objectAtIndex:index]];
if ([passTest evaluateWithObject:haystack]) {
cnt = cnt + 1;
}
}
if (cnt > 1 && [haystack length] > 5 && [haystack length] < 13)
{
return 1;
}
else
{
return 0;
}
}
And a sample IDEONE demo:
NSString * s = #"123DF4ffg";
NSLog(#"Result: %d", checkPassword(s));
// => Result: 1
Note that it is possible to write a single pattern for this, but it will be rather long and clumsy as you need to define all possible pairs of lookahead alternatives.
you can try with this
^(?:(?=.*[a-z])(?:(?=.*[A-Z])(?=.*[\\d\\w])|(?=.*\\w)(?=.*\\d))|(?=.*\\w)(?=.*[A-Z])(?=.*\\d)).{6,12}$;

Expected method to read array element not found of object type 'NSString *'

- (IBAction)convert_button:(id)sender {
NSString * binary_input = _binary_field.text;
if(binary_input!=NULL)
{
int count, total, i, j, tmp;
total = 0;
count = binary_input.length;
for (i = 0; i <= count; i++) {
if (binary_input[count-i] == '1') {//error here saying Expected method to read array element not found of object type 'NSString *'
tmp = 1;
for (j = 1; j < i; j++)
tmp *= 2;
total += tmp;
}
}
long decimal_value = strtol([binary_input UTF8String], NULL, 2);
NSString *resultString = [[NSString alloc]
initWithFormat: #"%li", decimal_value];
_decimal_output.text = resultString;
}
}
I am getting an error as commented in the code at my if statement that error says Expected method to read array element not found of object type 'NSString *'
I am new to objective-c and have mainly have done c++, any help is appreciated in how to fix the error, the logic makes sense to me.
for (i = 0; i <= count; i++) {
if (binary_input[count-i] == '1')
In your first iteration through the loop, you'll be looking for the element in spot -1, which is outside the bounds of an array.

Comparing objects of NSArray1 to NSArray2 [duplicate]

This question already has answers here:
Finding out NSArray/NSMutableArray changes' indices
(3 answers)
Closed 8 years ago.
Here is what i need it to do.
NSArray has 10 objects
NSArray2 has the same 10 objects but in different indexes.
I need to compare if NSArray1 index:5 matches NSArray2 index:5 if not tell me if the object has moved up or down in the NSArray2, same for every other object inside that array.
The objects have the following properties: id and name.
Any suggestion on how i can accomplish this?
If you have RAM to spare you could build a map from the object's id to its index in array 1, then scan array 2 and compare, like:
NSMutableDictionary *map = [[NSMutableDictionary alloc] init];
for (NSUInteger j = 0; j < array1.count; j++) {
id object = array1[j];
map[object.id] = #(j);
}
for (NSUInteger j = 0; j < array2.count; j++) {
id object = array2[j];
id identifier = object.id;
NSUInteger array1Index = [map[identifier] unsignedIntegerValue];
// Compare array1Index to j here.
}
That'll let you compare with a running time that grows like the number of objects in the arrays, but note that you have to spend some extra RAM to make that map. You could compare with only constant RAM costs if you're willing to spend more time:
for (NSUInteger j = 0; j < array1.count; j++) {
id object = array1[j];
NSUInteger k = [array2 indexOfObject:object];
// Compare j and k, note that k could be NSNotFound.
}
And that should have a running time that grows like the product of the array counts.

Compare and get the Index from NSMutableArray

I have an array, which is a collection of arrays.
say
(
( 1, x, y, a),
( 2, m, n, o),
( 3, s, t, u, v, w)
)
If I want to get the index of m, how do I get it?
Can that be done using NSMutableArray contains:(id) ?
Check with this code:
int arrayPos = 0;
for (NSArray *tempArray in collectionArray)
{
if(tempArray containsObject:#"m")
{
int indexOfWord = [tempArray indexOfObject:#"m"];
NSlog(#"Array pos: %d Index is : %d",pos, indexOfWord);
}
pos++;
}
In the for loop you get each array that is added in the collection array. After that you are checking the existence of the word in the array. If it is present you are displaying it.
Also printing the position of array also.
The element is situated at:
NSString *str = [[collectionArrayObjectAtIndex:pos] objectAtIndex:indexOfWord];
NSArray *arrWithArr;
NSArray *arrWithM;
NSInteger arrIndex;
for (int i = 0; i< [arrWithArr count]; i++) {
if ([[arrWithArr objectAtIndex:i] containsObject:#"m"]) {
arrIndex = i; break;
}
}
NSInteger mIndex = [[arrWithArr objectAtIndex:arrIndex]indexOfObject:#"m"];
The function you're talking about, "[NSMutableArray contains:]" will tell you if object "B" exists in an array of {A, B, C}.
What you really need to do is come up a second function that does a fast enumeration through your parent array and then does the "contains" thing on the sub-arrays. If it finds "m", return "YES" and the index of that entry in the parent array.

iOS NSMutableString returns weird character

I make an array of several words, then I put all words into a mutable string, and then a mutable array of every character of this string. However, "wordSplitted" that is a mutable array of the characters give random scrambled characters at each startup of app in Simulator, showing in the log:
2012-07-22 09:12:01.108 xxx[4484:b603] wordSplitted contains: (
"",
"",
"\U221e",
"-",
G,
N,
P,
"",
""
)
It should be b, i, l, h, u, k, s, k, j.
NSArray *threeWords = [NSArray arrayWithObjects:#"bil", #"huk", #"skje", nil];
NSMutableString *allTogether = [[NSMutableString alloc]init];
/*
for (NSString *s in threeWords)
{
[allTogether appendString:s];
}
*/
for (int b = 0; b < [threeWords count]; b++)
{
[allTogether appendString:[threeWords objectAtIndex:b]];
}
NSLog (#"allTogether contains %#", allTogether);
//[threeWords release];
[allTogether release];
NSMutableArray *wordSplitted = [[NSMutableArray alloc]init];
//this function gives me headache
for (int a = 0; a < 9; a ++)
{
//gives also scrambled characters
//[wordSplitted addObject:[NSString stringWithFormat:#"%C",[allTogether characterAtIndex:a]]];
NSRange range = {a,1};
[wordSplitted addObject:[allTogether substringWithRange:range]];
}
NSLog (#"wordSplitted contains: %#", wordSplitted);
How can I display normal characters like alphabets?
You call [allTogether release] but then reference it later on. Normally this would cause your program to crash, but something about the CFMutableArray backing makes this not always happen.
Basically, don't release the array until you're sure you're done using it.
about string splitting
http://www.idev101.com/code/Objective-C/Strings/split.html

Resources