IOS get 10 object from MutableArray [duplicate] - ios

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];

Related

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

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

Why can't I add a string to my NSMutableArray [duplicate]

This question already has answers here:
NSMutableArray addObject not working
(2 answers)
Closed 7 years ago.
I have created a function within my code that converts whatever is in the textfield of my app to append to my array. However, when I set a breakpoint in the code, it returns that my array, _descriptionArray is nil. Why is that?
I want whatever value is in the descriptionTextField.text to append to my _descriptionArray.
Thanks for the help!
-(NSMutableArray *)descriptionConversion{
[_descriptionArray addObject: (descriptionTextField.text)];
return _descriptionArray;
}
Did you ever initialize _descriptionArray? If not, be sure to initialize the array with _descriptionArray = [NSMutableArray array];. If you don't initialize the array, it will be nil. You can only add an object to an array; not to nil.

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)")

converting NSString to NSInteger in iOS [duplicate]

This question already has answers here:
Converting string to int changes the value
(2 answers)
Closed 8 years ago.
I have a string as
NSString *milliSeconds=#"1413808458821";
I tried the following to convert it to number, but None of them are giving correct value.
NSInteger number = [milliSeconds intValue];
NSInteger number = [milliSeconds integerValue];
NSInteger number = [milliSeconds longLongValue];
Please help me.
Thanks in advance.
This is probably because you are running this code on a 32bit architecture, thus NSInteger is always an int and the number you're trying to convert is too big for this data type.
Try manually specifying the type to long long.

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