[英]Objective-C How to add objects to NSMutableArray from other place?
Hello I'm junior in Objective-C and Swift programming.您好,我是 Objective-C 和 Swift 编程的大三学生。
I have NSMutableArray in ExampleMenuViewController.m or(and) SomeClass.m declared as vcTabs.我在 ExampleMenuViewController.m 或(和)SomeClass.m 中将 NSMutableArray 声明为 vcTabs。
NSMutableArray *vcTabs;
When I have two declares 'vcTabs' Xcode returns duplicate symbol '_vcTabs' (...)
当我有两个声明 'vcTabs' Xcode 返回
duplicate symbol '_vcTabs' (...)
How to add objects to an existing NSMutableArray init in other class (ExampleMenuViewController.m)?如何将对象添加到其他 class (ExampleMenuViewController.m) 中的现有 NSMutableArray init? I need append new objects from another class (SomeClass.m) to vcTabs (NSMutableArray).
我需要从另一个 class (SomeClass.m) 到 vcTabs (NSMutableArray) 的 append 个新对象。
I wrote in SomeClass.m this code:我在 SomeClass.m 中写了这段代码:
if ([Tools isNonullValueForKey:[dictionary valueForKey:@"additional_tabs"]]) {
additional_tabs = [dictionary valueForKey:@"additional_tabs"];
NSLog(@"additionalTabs count: %lu", [additional_tabs count]);
for (int i = 0; i < [additional_tabs count]; i++) {
if ([Tools isNonullValueForKey:[additional_tabs valueForKey:@"_id"]]) {
additional_tab_id = [[additional_tabs valueForKey:@"_id"] objectAtIndex:i];
}
if ([Tools isNonullValueForKey:[additional_tabs valueForKey:@"names"]]) {
NSDictionary *dic = [[additional_tabs valueForKey:@"names"] objectAtIndex:i];
_en_additional_tab_name = [dic valueForKey:@"en"];
_pl_additional_tab_name = [dic valueForKey:@"pl"];
}
if ([Tools isNonullValueForKey:[additional_tabs valueForKey:@"url"]]) {
additional_tab_url = [[additional_tabs valueForKey:@"url"] objectAtIndex:i];
//NSLog(@"additional_tab_url: %@", _additional_tab_url);
}
[vcTabs addObject:[[VCTab alloc] initWithIdAndTypeAndUrl:additional_tab_id :VCTabAdditional :additional_tab_url]];
NSLog(@"%@ %d %@ %@ %@ %@", @"pos", i, @"id: ", additional_tab_id, @"url: ", additional_tab_url);
}
}
ExampleMenuViewController method with initVCTabs带有 initVCTabs 的 ExampleMenuViewController 方法
- (void)initVCTabs {
vcTabs = [[NSMutableArray alloc] init];
[vcTabs removeAllObjects];
if ([Tools getBooleanUserDefault:@"example_visible_tab_attendees" :YES]) {
[vcTabs addObject:[[VCTab alloc] initWithType:VCTabAttendees]];
}
(...)
if ([Tools getBooleanUserDefault:@"example_visible_tab_user_info" :YES]) {
[vcTabs addObject:[[VCTab alloc] initWithType:VCTabUserInfo]];
}
if ([Tools getStringUserDefault:@"example_additional_tab_id" :@""]) {
NSString *additionalTabId = [Tools getStringUserDefault:@"conference_additional_tab_id" :@""];
NSString *additionalTabUrl = [Tools getStringUserDefault:@"conference_additional_tab_url" :@""];
NSLog(@"additionalTabId %@", additionalTabId);
NSLog(@"additionalTabUrl %@", additionalTabUrl);
[vcTabs addObject:[[VCTab alloc] initWithIdAndTypeAndUrl:additionalTabId :VCTabAdditional :additionalTabUrl]];
}
}
PS.附言。 If I use from ExampleMenuViewController I have only one tab with last object properties... but
additional_tabs
array have 17 objects.如果我从 ExampleMenuViewController 使用,我只有一个选项卡具有最后 object 个属性...但是
additional_tabs
数组有 17 个对象。
Do you have any ideas or advices?你有什么想法或建议吗? All the best for you everyone!
祝大家一切顺利!
When are you calling initVCTabs
?你什么时候打电话给
initVCTabs
?
When / how is the code in SomeClass.m
running? SomeClass.m
中的代码何时/如何运行?
For being a "junior in Objective-C and Swift programming" you seem to have a lot going on that you don't understand yet.作为“Objective-C 和 Swift 编程的初级”,您似乎有很多事情是您还不了解的。 Try creating a new project and learn how things work -- then implement that in your full project.
尝试创建一个新项目并了解其工作原理——然后在您的整个项目中实施它。
Here is a very, very simple example.这是一个非常非常简单的例子。 With the information you provided in your question, this may or may not relate directly - but it should give you an idea of where to go:
根据您在问题中提供的信息,这可能会或可能不会直接相关 - 但它应该让您了解 go 的位置:
SomeClass.h类.h
// SomeClass.h
// Created by Don Mag on 8/30/20.
#import <Foundation/Foundation.h>
NS_ASSUME_NONNULL_BEGIN
@interface SomeClass : NSObject
- (void)moreTabs:(NSMutableArray *)a;
@end
NS_ASSUME_NONNULL_END
SomeClass.m一些类.m
// SomeClass.m
// Created by Don Mag on 8/30/20.
#import "SomeClass.h"
@interface SomeClass()
@end
@implementation SomeClass
- (void)moreTabs:(NSMutableArray *)a {
[a addObject:@"B"];
[a addObject:@"C"];
[a addObject:@"D"];
[a addObject:@"E"];
}
@end
ExampleMenuViewController.h ExampleMenuViewController.h
// ExampleMenuViewController.h
// Created by Don Mag on 8/30/20.
#import <UIKit/UIKit.h>
NS_ASSUME_NONNULL_BEGIN
@interface ExampleMenuViewController : UIViewController
@end
NS_ASSUME_NONNULL_END
ExampleMenuViewController.m ExampleMenuViewController.m
// ExampleMenuViewController.m
// Created by Don Mag on 8/30/20.
#import "ExampleMenuViewController.h"
#import "SomeClass.h"
@interface ExampleMenuViewController ()
{
NSMutableArray *vcTabs;
}
@end
@implementation ExampleMenuViewController
- (void)viewDidLoad {
[super viewDidLoad];
// add a button to the view
//UIButton *b = [UIButton new];
UIButton *b = [UIButton buttonWithType:UIButtonTypeSystem];
[b setTitle:@"Tap Me" forState:UIControlStateNormal];
b.frame = CGRectMake(0, 0, 200, 50);
b.center = self.view.center;
[self.view addSubview:b];
[b addTarget:self action:@selector(btnTapped) forControlEvents:UIControlEventTouchUpInside];
[self initVCTabs];
[self logArray];
}
- (void)initVCTabs {
// instantiate NSMutableArray
vcTabs = [NSMutableArray new];
// add one object
[vcTabs addObject:@"A"];
}
- (void)btnTapped {
SomeClass *sc = [SomeClass new];
[sc moreTabs:vcTabs];
[self logArray];
}
- (void)logArray {
NSLog(@"vcTabs has %ld objects", [vcTabs count]);
for (NSString *s in vcTabs) {
NSLog(@"%@", s);
}
}
@end
When ExampleMenuViewController
loads, it will add a button to the center of the view, then instantiate the vcTabs
array and add one object - @"A"
.当
ExampleMenuViewController
加载时,它会在视图的中心添加一个按钮,然后实例化vcTabs
数组并添加一个 object - @"A"
。
We log the array to the debug console and see:我们将数组记录到调试控制台并查看:
vcTabs has 1 objects
A
When you tap the button, an instance of SomeClass
will be created, we call the moreTabs
method in that class, passing a reference to vcTabs
.当您点击按钮时,将创建
SomeClass
的实例,我们调用该 class 中的moreTabs
方法,传递对vcTabs
的引用。 That method will add 4 objects to the array - @"B" @"C" @"D" @"E"
.该方法会将 4 个对象添加到数组 -
@"B" @"C" @"D" @"E"
。
We then log the array to the debug console and see:然后我们将数组记录到调试控制台并查看:
vcTabs has 5 objects
A
B
C
D
E
@DonMag @唐马格
It's working but I have one question to you.它正在工作,但我有一个问题要问你。 I added for loop to increment.
我添加了 for 循环以增加。 When I add [additional_tabs count] is return nil.
当我添加 [additional_tabs count] 时返回 nil。 Why this method adds the same objects?
为什么这个方法添加相同的对象? My code in addMore function:
我在 addMore function 中的代码:
- (void)moreTabs:(NSMutableArray *)a {
for (int i = 1; i < additional_tab_count; i++) {
NSString *additionalTabId = [Tools getStringUserDefault:@"conference_additional_tab_id" :@""];
NSString *additionalTabUrl = [Tools getStringUserDefault:@"conference_additional_tab_url" :@""];
[a addObject:[[VCTab alloc] initWithIdAndTypeAndUrl:additionalTabId :VCTabAdditional :additionalTabUrl]];
}
}
Do you know how to get THIS "dictionary" or "additional_tabs".你知道如何获得这个“词典”或“additional_tabs”吗? I tried but always return nil (looks like a new other instance and is not this dictionary or additional_tabs from initFromJSON).
我试过但总是返回 nil(看起来像一个新的其他实例,而不是这个字典或来自 initFromJSON 的 additional_tabs)。 I only managed to make variable additional_tab_count and it's return 17 (correct count), but I'd rather have access to array from additional_tabs declared in if statements.
我只设法使变量 additional_tab_count 返回 17(正确计数),但我宁愿从 if 语句中声明的 additional_tabs 访问数组。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.