Xcode: XCTest cannot no compare two NSArray of numbers [duplicate] - ios

This question already has answers here:
XCTAssertEqual fails to compare two string values?
(3 answers)
Closed 2 years ago.
I'm trying to implement a unit test(XCTest) to compare two NSArray objects:
Here is my implementation:
- (void)testTwoNumsArrays {
NSArray *result = #[#20,#20];
NSArray *expecteResult = #[#20,#20];
XCTAssertEqual(result, expecteResult);
}
I'm getting this error:
((result) equal to (expecteResult)) failed: ("{length = 8, bytes = 0xb0cfc00001000000}") is not equal to ("{length = 8, bytes = 0x9032ce0001000000}")
Any of you know why I can not compare the two arrays. In Swift it works just fine.
I'll really appreciate your help.

The problem is you are comparing the pointers to the objects and not the objects themselves.
XCTAssertEqual is essentially doing a check like the following, which is only comparing the pointer values.
if (a != b) assert();
You want something more along the lines of
if (![a isEqual:b]) assert();
This can be achieved with the XCTAssertEqualObjects call.
More information:
XCTAssertEqual fails to compare two string values?
https://developer.apple.com/documentation/xctest/xctassertequalobjects

Related

Filtering with NSPredicate an Array of Array to see if It contains an Integer in Swift [duplicate]

This question already has answers here:
Using NSPredicate to filter array of arrays
(2 answers)
Closed 5 years ago.
Hi I am trying to learn a bit more about NSPredicate- and its quite tough as the resources are still based on ObjC. I would like a swift based answer if possible
Basically I am trying to use NSPredicate to see if the arrays below contain a "9" the code number for Male users.
I thus want to filter the below people array and only keep arrays that contain the number 9.
var malePerson1:[Int] = [1,4,5,6,11,9]
var malePerson2:[Int] = [3,5,6,7,12,9]
var femalePerson3:[Int] = [3,5,6,7,12,10]
var people = [malePerson1, malePerson2, femalePerson3]
I have got the solution working fine using a filter (see below)
//Solution working with Filter
// male gender search
var result = people.filter{$0.contains(9)}
print(result)
var resultFemale = people.filter{$0.contains(10)}
print(resultFemale)
but how Do I do something similar using predicates?
Below doesn't work - resultMale just returns a blank array when it should contain the two arrays: [maleperson1, maleperson2]. I think the problem is that its checking if 9 is contained in the 'people' array instead of checking the contents of the contained arrays.
Any ideas how to do what I am doing using NSPredicate to filter the integers in the array?
let malePred = NSPredicate(format: "9 IN %#",people)
//9 is the code for Male user
var resultMale = (people as NSArray).filtered(using: malePred)
You can use ANY to compare each nested array with NSPredicate.
let malePred = NSPredicate(format: "ANY self == 9")

IOS get 10 object from MutableArray [duplicate]

This question already has answers here:
What is an easy way to break an NSArray with 4000+ objects in it into multiple arrays with 30 objects each?
(3 answers)
Closed 7 years ago.
I have a NSMutableArray *allObject, my allObject have 22 objects inside.
And now I want to get 10 objects when I click button More.
I am using:
NSArray *arrrTemp = [arrObject subarrayWithRange:NSMakeRange(from_index, 10)];
1st, I got 10 object from allObject
2nd, I got 10 next object from allObject
It's OK.
But, 3rd: It's crash app. I think subarrayWithRange:NSMakeRange(from_index, 10) ---> 10 is problem.
How to I can resolve this problem?
You need to check if there are at least 10 objects left.
NSInteger length = MIN(10, arrObject.count - from_index);
NSRange range = NSMakeRange(from_index, length);
NSArray *arrrTemp = [arrObject subarrayWithRange:range];

NSMutableArray to Array of String Conversion Swift iOS [duplicate]

This question already has answers here:
How can I cast an NSMutableArray to a Swift array of a specific type?
(7 answers)
Closed 7 years ago.
I need to populate data in a table view. How can I convert [NSMutableArray?] to [(String)] in Swift iOS
println(self.subcategory[index]!) i.e., an [NSMutableArray?], where
index is Int shows:
(
Operations,
Marketing,
"Public Relations",
"Human Resources",
Advertising,
Finance,
Hotels,
Restaurants,
Other
)
Try with this.
let myArray : NSMutableArray = ["Operations"]
myArray.addObject("Marketing")
myArray.addObject("Public Relations")
myArray.addObject("Human Resources")
myArray.addObject("Advertising")
myArray.addObject("Finance")
myArray.addObject("Hotels")
myArray.addObject("Restaurants")
myArray.addObject("Other")
NSLog("\(myArray)")
var string = myArray.componentsJoinedByString(",")
NSLog("\(string)")

Set objects to empty NSDictionary in Swift [duplicate]

This question already has answers here:
Swift dictionary bug?
(6 answers)
Closed 8 years ago.
I can create an NSDictionary
var namesDictionary=Dictionary<String,String>()
namesDictionary["Jacob"] = "Marry"
But, when I create a empty dictionary like coded below, line 1 i okie, but line 2 (adding values) throws an error.
var namesDictionary =[:]
namesDictionary["Jacob"] = "Marry"
Error is "Cannot assign to the result of this expression". Is there any other way to assign the values.
It looks like it's an issue with swift interpreting the type of your dictionary. Try explicitly typing your empty dictionary.
var namesDictionary: Dictionary<String, String> = [:]
namesDictionary["Jacob"] = "Marry"
I think a better use for [:] is for emptying an already defined dictionary. If you add a third line namesDictionary = [:], you will be able to call namesDictionary["Jacob"] = "Marry" again since the compiler knows what type of dictionary it is from the inital declaration.

Need to check if NSArray object contains specific text. [duplicate]

This question already has answers here:
Finding a substring in a NSString object
(7 answers)
NSString search whole text for another string
(3 answers)
Closed 9 years ago.
I am performing a method call that places any objects found in the background into an NSArray object called "objects".
When I NSLog the counts property of "objects" it tells me that the array contains 1 object which is correct.
When I NSLog the NSArray object called "objects" it prints out the following:
(
"<PFUser:nzyjeVFgjU:(null)> {\n email = \"hahshshs#aol.com\";\n username = hzhjsshhsppppp;\n verificationCode = 6449;\n}"
)
Here is my problem. I need to create an if statement that takes another value I already have and compares it to the 4 digit number that verificationCode is equal to eg. Above in the code it says "verificationCode = 6449"
I am basically trying to compare a 4 digit code that I already have to the verificationCode that is contained in this single NSArray object.
I know how to write if statements, but i have no idea how to specifically focus on "verificationCode = 6449" since it is just text inside of a string.
I have been trying to figure out a way to do this for an hour now so any help is greatly appreciated thank you.
Just found the answer here: https://stackoverflow.com/a/7574136/3117509
I tried searching earlier but was searching for "search array for string" when I should have been searching for something like "search for string within a string" or "search for text within a string."
If I understand your question correctly, you're trying to iterate through an array and find if a string is there? Don't have access to a Mac right now, so I'm not sure if this will compile.
NSArray* someArray = [[NSArray alloc] initWithObjects:#"test", #"test1", #"test2", nil];
for(int i = 0; i < (int)[someArray count]; i++) {
if([[someArray objectAtIndex:i] isEqualToString #"test"]) {
//match found. handle the match
}
}

Resources