Mapping multiple Arrays in one Array [closed] - ios

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I do have two Arrays one array with list of items:
One array with string objects
and one with Array objects
NSArray * a=[1,2,nil];
NSArray * b=[[abc],[def],[ijk],[lmp], nil];
I want to return Array by mapping Array "a" object "1" to array "b" with objects "[abc],[def]"
and Array "a" object "2" to array "b" with objects "[ijk],[lmp]"
I know I can achieve it in NSDictionary but i want to return NSArray not NSDictionary.
or any alternative method to do it.

I think it's more about data structures knowledges?
I see that your pattern is to increase two index.
So, it's something you want?
NSMutableArray *object;
NSUInteger indexOfA = [a indexOfObject:#1]; // get the index of the object from A
for (NSUInteger index = 0; index < 2; index++) {
[object addObject:b[indexOfA * 2 + index]];
}
Then, you can pull out what you want from b.

Related

How to write Objective-C to Swift 4 below method? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
Below is Objective-c Code.
I need this method in Swift, how can I write in Swift4?
I'm new to swift, please help.
-(NSMutableArray)dataArray
{
if(!_dataArray)
{
_dataArray = [NSMutableArray new];
}
return _dataArray;
}
#property(nonatomic,strong) NSMutableArray *dataArray;
The above method you used in Objective-C is to initialise the memory to dataArray when it is being used. It is generally used to minimise memory consumption.
In swift, this process is being handled by lazily instantiated properties, by putting a keyword lazy before the property. It will allocate the memory to property only when it is firstly being used.
lazy var dataArray = [String]()
Note: In swift, use swift based array rather than NSArray/ NSMutableArray
If you want some customisation to your dataArray, you can do it like:
lazy var dataArray: [String] = {
var temp = [String]()
temp.append("John Doe")
return temp
}()
You can refer the link: http://mikebuss.com/2014/06/22/lazy-initialization-swift/

How dynamically initialise object in Swift 3 [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I use below line of code to allocate an Object(Suppose my Object name is Car) dynamically.
[self initliazieObject:[Car class]]
- (id)initliazieObject:(Class)model{
id record = [[model alloc] init];
return record;
}
How I can do this in swift 3.
Exactly as in Objective-C. Try this in a playground:
class Car : NSObject {}
func factory(type:NSObject.Type) -> NSObject {
return type.init()
}
let c = factory(type:Car.self)
print(type(of:c)) // Car
(We can get fancy and do clever things with generics or Self to specify the type of the returned object more precisely, but my goal in this code is simply to do things the dumb way, just like Objective-C.)

How to extract an Array out of NSArray? [duplicate]

This question already has answers here:
NSArray of united Arrays
(6 answers)
Closed 8 years ago.
I have been using an NSDictionary to store my JSON response. Now I tried to store them inside an array, so I can iterate over each values.
I used: valueForKeyPath and now each of those values are inside an NSArray.
The problem is instead of returning self.myarray.count (3), I only get (1). It wrote
the NSDictionary Keys into myarray at Index 0. So I have an Array inside myarray at Index 0 containing values (2,5,3).
Is there a way to quickly fix this and putting this like: index 0 : 2 , index 1: 5 , index 2: 3
and not index 0 : (2,5,3)?
Thanks I am still learning Obj-C.
As I understand you have perform something like following: [self.myarray addObject: dict.keys];
Use following instead: self.myarray = dict.keys;
If you do this only because you want to iterate over each value you could do the same with dictionary: for(id key in dict) or for(id key in dict.keys) iterates over each key. for(id value in dict.values) iterates over each value.

Can't Assign Expression in iOS [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 8 years ago.
Improve this question
I've got an NSMutableArray called Letters, and it contains instances of a UIImageView subclass. I am trying to loop through them and assign them new values
for (int i=0 ;i<26;i++)
{
[letters objectAtIndex:i] = [[typeLetters alloc]initWithManager:self atX:startX andY:startY withSideLenght:48 andName:[letterNames objectAtIndex:i] andPng:[letterNames objectAtIndex:i]];
}
This doesn't work though. Xcode says "Cannot assign to expression", but when I try to assign new values to each array item individually, outside of the array, it works. Do you know why this would be?
EDIT: Nevermind. I'm sorry. I should have looked into arrays a bit more.
The method you're looking for is.-
- (void)replaceObjectAtIndex:(NSUInteger)index withObject:(id)anObject;
for (int i=0 ;i<26;i+=1) {
[letters replaceObjectAtIndex:i withObject:[[typeLetters alloc]initWithManager:self atX:startX andY:startY withSideLenght:48 andName:[letterNames objectAtIndex:i] andPng:[letterNames objectAtIndex:i]]];
}
array notation works: letters[i] = blah
But you should probably be using fast enumeration:
for(id letter in letters) {
letter = blah;
}
You need to use replaceObjectAtIndex:withObject:, but you should really use the new array syntax:
letters[i] = ...

Design pattern for multiple buttons [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
What is the better way to do this (the more buttons the longer the code is getting)?
Does one have to add to an array (then use reflection to look at the variable title and pull out the proper string?
//Load defaults
_n1From = [HelperMethods getObjectUserDefault:AS_N1_FROM_UD];
_n1To = [HelperMethods getObjectUserDefault:AS_N1_TO_UD];
_n2From = [HelperMethods getObjectUserDefault:AS_N2_FROM_UD];
_n2To = [HelperMethods getObjectUserDefault:AS_N2_TO_UD];
_unionRate = [HelperMethods getObjectUserDefault:AS_UNION_RATE_UD];
_unionHours = [HelperMethods getObjectUserDefault:AS_UNION_HOURS_UD];
_nonUnionRate = [HelperMethods getObjectUserDefault:AS_NON_UNION_RATE_UD];
_nonUnionHours = [HelperMethods getObjectUserDefault:AS_NON_UNION_HOURS_UD];
Put buttons into IBOutletCollection, assign each button a unique tag value, and then use the tag to look up the argument in an NSArray that mapps the tag to the parameter. Here is an example:
NSArray *tagToArg = #[#AS_N1_FROM_UD, #AS_N1_TO_UD, #AS_N2_FROM_UD, ...];
for (UIButton *b in allButtonsOutletCollection) {
[HelperMethods configureButton:b withData:tagToArg[b.tag]];
}
The button with the tag zero will get the argument AS_N1_FROM_UD; the button with the tag 1 will get AS_N1_TO_UD, and so on.

Resources