NSDictionary转NSArray,无法正常显示?

发布于 2024-12-09 10:29:22 字数 1566 浏览 5 评论 0原文

我有一个签名页,用户可以在其中签名。 然后需要将其保存到 NSDictionary,但我想将键列表调用为 TableView 中每行或单元格的文本。 所以:

“viewImage = 保存为对象到键:随机数”

这部分有点简单,困难的部分是当我在 TableView 的另一个页面上调用它时。 它退出应用程序并出现错误“SIGABRT”。现在我的所有代表都已到位并正在工作……我相信。 现在这里是一些示例代码:

FirstPage.m

UIImagePNGRepresentation(viewImage);
NSMutableArray *innerArray = [[NSMutableArray array]init];
[innerArray addObject:viewImage];
[SignatureSave setObject:innerArray forKey:@"5599"];

足够简单,但不会给我错误。

SecondPage.m

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
FirstPage *appShare = (FirstPage *)[[UIApplication sharedApplication] delegate];
NSArray *dataDuplicate = [[NSArray alloc]init ];
dataDuplicate = [appShare.SignatureSave allKeysForObject:@"innerArray"];
static NSString *CellIdentifier = @"Cell";
NSLog(@"%@",dataDuplicate);
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell == nil)
{
    cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero]autorelease];
}
if (dataDuplicate != nil) {
     cell.textLabel.text = [dataDuplicate objectAtIndex:indexPath.row];
}
else
{
    UIAlertView *CellAlert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Error Loading content, Try Again Later." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
    [CellAlert show];
    [CellAlert release];
}

return cell;
}
@end

现在,我如何将 viewImage 保存到 NSDictionary,以便能够在 SecondPage 上调用它并在 TableVIew 中显示对象的名称?

I have a signature page, were the user can sign their name.
It needs to then be saved to NSDictionary, but i want to call a List of the keys to be text in a TableView for each row or cell.
so:

"viewImage = saved as object to key:Random Number"

That parts somewhat easy, the hard part is when i call it on the other Page to the TableView.
It Exits the App with Error"SIGABRT". Now all my Delegates are in place and working...i believe.
now heres some example code:

FirstPage.m

UIImagePNGRepresentation(viewImage);
NSMutableArray *innerArray = [[NSMutableArray array]init];
[innerArray addObject:viewImage];
[SignatureSave setObject:innerArray forKey:@"5599"];

simple Enough, but doesnt give me an error.

SecondPage.m

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
FirstPage *appShare = (FirstPage *)[[UIApplication sharedApplication] delegate];
NSArray *dataDuplicate = [[NSArray alloc]init ];
dataDuplicate = [appShare.SignatureSave allKeysForObject:@"innerArray"];
static NSString *CellIdentifier = @"Cell";
NSLog(@"%@",dataDuplicate);
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell == nil)
{
    cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero]autorelease];
}
if (dataDuplicate != nil) {
     cell.textLabel.text = [dataDuplicate objectAtIndex:indexPath.row];
}
else
{
    UIAlertView *CellAlert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Error Loading content, Try Again Later." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
    [CellAlert show];
    [CellAlert release];
}

return cell;
}
@end

Now, How do i get the viewImage to save to the NSDictionary, to be able to call it on the SecondPage and display the name of the objects in the TableVIew?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

用心笑 2024-12-16 10:29:22

我真的不明白你到底有什么问题。

首先,你的字典是一个保留对象吗?

//FirstPage.h

@property (nonatomic, retain) NSDictionary *SignatureSave;

//FirstPage.m

@synthesize SignatureSave;
…
UIImagePNGRepresentation(viewImage);
NSMutableArray *innerArray = [NSMutableArray array]; // using "array" is equivalent to alloc-init-autorelease
[innerArray addObject:viewImage];
[self.SignatureSave setObject:innerArray forKey:@"5599"];

// OR setting the array directly:

UIImagePNGRepresentation(viewImage);
NSArray *innerArray = [NSArray arrayWithObject:viewImage];
[self.SignatureSave setObject:innerArray forKey:@"5599"];

// OR even setting the image directly to the dictionary:

UIImagePNGRepresentation(viewImage);
[self.SignatureSave setObject:viewImage forKey:@"5599"];

现在,如果你通过在前面写self.来访问该对象,它将调用retain,你的对象将保持活动状态。否则它将在方法结束时自动释放。这将解决您的字典在创建表视图时可能不存在/不可用的问题,并且您不必使用单例。

您想使用此代码访问什么?

FirstPage *appShare = (FirstPage *)[[UIApplication sharedApplication] delegate];

使用[[UIApplication sharedApplication] delegate]您可以获得应用程序委托(显然)。这些是 MyAppNameAppDelegate 文件,但您将其视为 FirstPage 类。
只需使用 NSLog() 来检查您是否获得了正确的类,即您期望的类。

NSLog(@"%@", [[[UIApplication sharedApplication] delegate] class]);

这里有潜在的泄漏,您分配了初始化但从未释放它:

NSArray *dataDuplicate = [[NSArray alloc]init ];
dataDuplicate = [appShare.SignatureSave allKeysForObject:@"innerArray"];

此外,您可以简化它(将自动释放):

NSArray *dataDuplicate = [appShare.SignatureSave allKeysForObject:@"innerArray"];

这里还有另一个问题。
为什么您将对象的所有键称为@"innerArray"

您没有这样的对象,而且在更多情况下它是错误的。 innerArray 是您之前在 FirstPage.m 中命名的数组,但这只是为了让您作为开发人员更好地记住该变量。编译后它无论如何都会有一个神秘的名字。如果您愿意,您可以访问您的密钥@"5599",但我认为您不想要这个。在您的情况下,您想要访问字典的所有键,因此只需调用:

NSArray *dataDuplicate = [appShare.SignatureSave allKeys];

现在您将拥有一个包含字典所有键的数组,您可以像使用 objectAtIndex: 一样访问它们。

NSString *keyName = [dataDuplicate objectAtIndex:indexPath.row];
cell.textLabel.text = keyName;
id theObject = [appShare.SignatureSave objectForKey:keyName]; // for example the image

告诉我这是否能解决您的问题,或者告诉我我是如何误解您的问题的。

I really don't understand whats your problem exactly.

first of all, is your dictionary a retained object?

//FirstPage.h

@property (nonatomic, retain) NSDictionary *SignatureSave;

//FirstPage.m

@synthesize SignatureSave;
…
UIImagePNGRepresentation(viewImage);
NSMutableArray *innerArray = [NSMutableArray array]; // using "array" is equivalent to alloc-init-autorelease
[innerArray addObject:viewImage];
[self.SignatureSave setObject:innerArray forKey:@"5599"];

// OR setting the array directly:

UIImagePNGRepresentation(viewImage);
NSArray *innerArray = [NSArray arrayWithObject:viewImage];
[self.SignatureSave setObject:innerArray forKey:@"5599"];

// OR even setting the image directly to the dictionary:

UIImagePNGRepresentation(viewImage);
[self.SignatureSave setObject:viewImage forKey:@"5599"];

now if you access the object by writing self. in front it will call the retain and your object will stay alive. Otherwise it would be autoreleased at the end of the method. This will fix the problem that your dictionary is maybe not present/available at table view creation and you don't have to use a singleton.

what are you trying to access with this code?

FirstPage *appShare = (FirstPage *)[[UIApplication sharedApplication] delegate];

with [[UIApplication sharedApplication] delegate] you get your application delegate (obviously). These are the MyAppNameAppDelegate files but you treat it as a FirstPage class.
Just NSLog() to check you get the right class, the one you expect.

NSLog(@"%@", [[[UIApplication sharedApplication] delegate] class]);

here you have a potential leak, you alloc-init but never release it:

NSArray *dataDuplicate = [[NSArray alloc]init ];
dataDuplicate = [appShare.SignatureSave allKeysForObject:@"innerArray"];

furthermore you can simplify it (will be autoreleased):

NSArray *dataDuplicate = [appShare.SignatureSave allKeysForObject:@"innerArray"];

and here you have another issue.
Why do you call all keys for the object @"innerArray"?

you don't have such an object and it's in many more cases wrong. innerArray was your previously named array in FirstPage.m but it is only for you as a developer to remember the variable better. After compilation it will have a cryptic name anyway. You could access your key @"5599" if you like but I don't think you want this. In your case you want to access all keys of the dictionary so simply call:

NSArray *dataDuplicate = [appShare.SignatureSave allKeys];

now you will have an array with all keys of your dictionary and you can access them like you do with objectAtIndex:.

NSString *keyName = [dataDuplicate objectAtIndex:indexPath.row];
cell.textLabel.text = keyName;
id theObject = [appShare.SignatureSave objectForKey:keyName]; // for example the image

Tell me if this solves your problems or tell me how I misunderstood your question.

不语却知心 2024-12-16 10:29:22

我发现这个问题的答案实际上很简单,

我最终选择了单例方法而不是全局变量方法。

现在,单例方法看起来很可怕,但它很简单,请参阅此处。

我注意到单例方法与全局方法的主要区别是,
全局方法需要大量的转换和重新转换。
尽管单例方法正在处理多个页面或类上的单个对象。

现在我希望这也能更好地帮助将来的人们!

I found the answer to this to be quit simple actually,

I ended up going with the Singleton Method instead of the Global Variable Method.

Now the Singleton Method looks terrifying but its quit simple, See here.

The main difference i noticed from the singleton method to the global method is,
Global method takes a lot of converting and re-converting.
Though the Singleton Method is working with a single object over many pages or classes.

Now i hope this will better assist people in the future also!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文