我有一个包含许多对象的NSMutableArray.
如果对数组进行了更改会发生什么,而我使用[NSMutableArray arrayWithArray:someArray]制作数组的副本;
例如:如果一个对象在复制过程中从数组中删除了一个对象?
我不确定如何测试这种情况.
编辑:对象未释放(因为它们保留在其他地方).我只是将这个数组用作查找表.
我正在尝试为列出的对象创建一个带有自定义标头组(父节点)的NSOutlineVew.(注意:我有基于单元格的NSOutlineView).例如,它看起来像Xcode"Navigator"或Numbers侧边栏.我使用每个类别的分隔属性的默认组,但它看起来不像我想要的那样.我需要一个父节点(单元格),我可以直观地调整(添加一个控件元素和图像).
我尝试通过将一组对象传递给NSDictionary来完成此操作,为每个组提供特定的特定键.结果,通过NSLog一切都正确显示,但是这个变量作为程序NSOulineView的源传输失败.
ProjectViewController.h
@interface ProjectViewController : NSViewController <NSOutlineViewDataSource, NSObject> {
IBOutlet NSOutlineView *outlineView;
FSEntity *content;
}
@property (readonly, assign) NSMutableArray *objects;
@end
Run Code Online (Sandbox Code Playgroud)
ProjectViewController.m
@implementation ProjectViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Initialization code here.
// Setting default path to the local file or directory
NSString *home = NSHomeDirectory();
NSURL *url = [[NSURL alloc] initFileURLWithPath:home];
content = [[FSEntity alloc] initWithURL:url];
[self defineContentNSOutlineView];
NSLog(@"Array: %@",_objects);
// Basic ?onfiguration an instance NSOutlineView
[self configurationNSOutlineView]; …Run Code Online (Sandbox Code Playgroud) 奇怪的行为NSMutableArray.
我已经创建了对象并填充了它.
NSMutableArray *array = [[NSMutableArray alloc]
initWithObjects:@"1",@"2",@"3",@"4", nil];
[array removeObjectAtIndex:0];
Run Code Online (Sandbox Code Playgroud)
在删除它之前看起来像:
array NSMutableArray * 0x1040b5e0
[0] id 0x00088a44 @"1"
[1] id 0x00088a54 @"2"
[2] id 0x00088a64 @"3"
[3] id 0x00088a74 @"4"
Run Code Online (Sandbox Code Playgroud)
删除FIRST元素后:
array NSMutableArray * 0x1040b5e0
[0] id 0x00000000
[1] id 0x00088a54 @"2"
[2] id 0x00088a64 @"3"
Run Code Online (Sandbox Code Playgroud)
我在这做错了什么?
在Objective C中我使用了来自各种线程的NSMutableArray实例,并且我正在使用@synchronized来使其线程安全.目前我对这个数组的所有访问都受到@synchronized块的保护,甚至是objectAtIndex:方法.不过我想知道哪些方法确实需要用@synchronized保护.我需要保护读取权限吗?
如果'ObjectAtIndex'没有受到保护并且在'removeObject'的同时被调用会发生什么?
如果所有方法都受@synchronized保护,性能如何?(我正在写一个tcp/udp游戏服务器,如果它会降低性能或生成锁定,我真的不想过度保护这些数组).
例如,我想'containsObject:'方法将枚举以查找对象,并且我应该避免在另一个线程中对'removeObject:'进行concurent调用.
也许一个好的解决方案是拥有太多不同的锁(用于读写访问)......
欢迎提供帮助和建议!
非常感谢.
请在下面找到示例代码来说明:
@interface TestClass : NSObject
{
NSMutableArray * array;
}
@end
@implementation TestClass
- (id)init
{
self = [super init];
if (self)
{
array = [NSMutableArray array];
}
return self;
}
-(id)objectAtIndex:(NSUInteger)index
{
@synchronized(array) **// IS IT USEFUL OR NOT ??**
{
return [array objectAtIndex:index];
}
}
-(void)removeObject:(id)object
{
@synchronized(array)
{
[array removeObject:object];
}
}
-(void)compute
{
@synchronized(array)
{
for (id object in array)
{
[object compute];
}
}
}
@end
Run Code Online (Sandbox Code Playgroud) 我想比较2 NSMutableArray并将不同的对象变为第三Array.我怎样才能做到这一点 ?
Array1可以循环对象.
Array1 = "a", "b","c","d","a","b","c";
Array2 = "a", "b", "c";
Run Code Online (Sandbox Code Playgroud)
然后结果
Array3 = "d";
Run Code Online (Sandbox Code Playgroud)
提前致谢
我需要一些帮助来理解如何使用NSArrays/NSMutableArrays作为属性.
目前,我采用的方式有两种不同的方式,如下所示.但它完全不完整,没有明确理解它的机制.哪一项是正确的还是不正确的.
方法1
.m file
interface()
@property (nonatomic, strong) NSMutableArray *arrayOfData;
implementation
<....other code>
self.arrayOfData = [NSMutableArray arrayWithCapacity:count];
[self.arrayOfData addObject:<my object>]
Run Code Online (Sandbox Code Playgroud)
方法2
.h file
@property (nonatomic, strong) NSArray *arrayOfData;
.m file
//Property setter
- (void)setListOfData:(NSMutableArray *)newList {
if (_arrayOfData != newList) {
_arrayOfData = [newList copy];
}
}
//Function which initializes the arrayOfData
NSMutableArray *newData = [[NSMutableArray alloc] init];
.....<code which adds data>
self.arrayOfData = newData;
Run Code Online (Sandbox Code Playgroud) 我正在制作的应用程序Polyline根据用户坐标绘制CLLocationManager(这些都保存在一起NSMutableArray)
当应用关闭时,当前折线消失.如何在应用程序启动时存储要在CoreData中检索的点数组?用户在启动应用程序后所采取的所有过去"路线"应该被恢复并覆盖在地图上(这可能相当多,因此CoreData似乎是我收集的最佳选择).
这是我用来从加载的坐标创建MKPolyline的代码
-(IBAction)didClickLoadCoordinates:(id)sender {
// get a reference to the appDelegate so you can access the global managedObjectContext
AppDelegate *appDelegate = [UIApplication sharedApplication].delegate;
NSFetchRequest *request = [[NSFetchRequest alloc] initWithEntityName:@"Route"];
NSError *error;
id results = [appDelegate.managedObjectContext executeFetchRequest:request error:&error];
if ([results count]) {
polyLine = (Route *)(results[0]);
NSArray *coordinates = polyLine.coordinates;
int ct = 0;
for (CLLocation *loc in coordinates) {
NSLog(@"location %d: %@", ct++, loc);
}
// this copies the array to your mutableArray …Run Code Online (Sandbox Code Playgroud) 我正在尝试NSMutableArray在Swift中创建一个子类,它支持一个数组文字初始化器.
我可以创建一个简单的子类:
class MyArray: NSMutableArray {
}
Run Code Online (Sandbox Code Playgroud)
初始化工作正常:
let arrayInstance: MyArray = MyArray()
Run Code Online (Sandbox Code Playgroud)
但是,如果我尝试使用数组文字:
let arrayInstance: MyArray = ["one"]
Run Code Online (Sandbox Code Playgroud)
它在运行时失败了[NSMutableArray initWithCapacity:]: method only defined for abstract class..
我可以添加此初始化程序,因此我还必须添加其他所需的初始化程序:
class MyArray: NSMutableArray {
override init(capacity numItems: Int) {
super.init(capacity: numItems)
}
required init(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
required convenience init(arrayLiteral elements: AnyObject...) {
fatalError("init(arrayLiteral:) has not been implemented")
}
}
Run Code Online (Sandbox Code Playgroud)
不幸的是,第二个必需的初始化程序产生以下编译器错
来自扩展的声明无法覆盖
看起来我被卡住了!任何人都可以建议一种方法来创建一个NSMutableArray支持数组文字的子类吗?否则它会回到Objective-C对我来说!
有许多类似标题的问题,但没有一个问题对我有帮助.
但我可以将这个'NSGenericException'的解决方案联系起来,原因:Collection <__ NSArrayM:0x7fabb400>在 我的问题被枚举问题时发生了变异.因为在我添加时会发生这种终止,所以用数组删除字符串对象.
错误:
因未捕获的异常'NSGenericException'而终止应用程序,原因:'***Collection <__ NSArrayM:0x7fa1f2711910>在枚举时发生了变异.
第一次抛出调用堆栈:
(
0 CoreFoundation 0x00000001087b8c65 __exceptionPreprocess + 165
1 libobjc.A.dylib 0x0000000108422bb7 objc_exception_throw + 45
2 CoreFoundation 0x00000001087b85c4 __NSFastEnumerationMutationHandler + 132
3 0x0000000107e24cf5 -[ProfessionalRegSecond tableView:didDeselectRowAtIndexPath:] + 773
4 UIKit 0x0000000109759d71 -[UITableView _deselectRowAtIndexPath:animated:notifyDelegate:] + 347
5 UIKit 0x000000010974deea -[UITableView _userSelectRowAtPendingSelectionIndexPath:] + 187
6 UIKit 0x000000010968062c _applyBlockToCFArrayCopiedToStack + 314
7 UIKit 0x00000001096804a6 _afterCACommitHandler + 533
8 CoreFoundation 0x00000001086ebca7 __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 23
9 CoreFoundation 0x00000001086ebc00 __CFRunLoopDoObservers + …Run Code Online (Sandbox Code Playgroud) nsmutablearray ×10
objective-c ×7
ios ×3
cocoa-touch ×1
core-data ×1
iphone ×1
nsarray ×1
persist ×1
polyline ×1
store ×1
swift ×1
synchronized ×1
uitableview ×1