Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/36.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
iphone中的Nslog imagename_Iphone_Objective C_Ios_Arrays - Fatal编程技术网

iphone中的Nslog imagename

iphone中的Nslog imagename,iphone,objective-c,ios,arrays,Iphone,Objective C,Ios,Arrays,我有一个带有图像的NSArray。图像名称是A、B、C、D。我需要NSLog数组中这些图像的名称 NSLog(@"Name == %@", [Array objectAtIndex:1]); 我必须用什么来代替它呢?一个UIImage不存储它的文件名,如果你想跟踪创建它们的文件名,你也需要存储它们。如果你想NSLog整个NSArray使用这个: NSArray *_array = // your array NSLog(@"array == %@", _array); 更新:#1 可用的方法

我有一个带有图像的
NSArray
。图像名称是A、B、C、D。我需要
NSLog
数组中这些图像的名称

NSLog(@"Name == %@", [Array objectAtIndex:1]);

我必须用什么来代替它呢?

一个
UIImage
不存储它的文件名,如果你想跟踪创建它们的文件名,你也需要存储它们。

如果你想
NSLog
整个
NSArray
使用这个:

NSArray *_array = // your array
NSLog(@"array == %@", _array);
更新:#1

可用的方法之一是以下方法,而不是仅用
UIImage
对象填充
NSMutableArray
,您应该向
NSMutableArray
添加具有以下内容的“NSDictionary”对象:

NSMutableArray *_array = [NSMutableArray array];

// you could put this part inside a loop if you like
NSString *_imagePathWithName = @"...";
UIImage *_newImage = [UIImage imageNamed:_imagePathWithName]; // when you load it from you application bundle
// or [UIImage imageWithContentsOfFile:_imagePathWithName]; // loading from other place
NSDictionary *_imageDictionary = [NSDictionary dictionaryWithObjectsAndKeys:_newImage, @"keyForUIImage", _imagePathWithName, @"keyForFullPathAndName", nil];
[_array addObject:_imageDictionary];
当您想从数组中读取名称时

for (NSDisctionary *_dictionary in _array) {
    UIImage *_image = (UIImage *)[_dictionary valueForKey:@"keyForUIImage"];
    NSString *_fullPathWithName = (NSString *)[_dictionary valueForKey:@"keyForFullPathAndName"];
    // do whatever you'd like with the images and the path
}

你不能。如果A、B、C、D是实例变量,那么它们是唯一可以控制的对象,比如

if ((UIImage*)[Array Objectatindex:1] == A) 
   bla-bla-bla you know that it's "A"

一旦分配,就无法访问图像名称。

根据wattson的建议,这是不可能的。为此,您需要使用另一个数组,在该数组中可以保存imageName。否则您可以使用
NSMutableDictionary
并将imageName保存为键,将Array对象保存为其对象,以便以后阅读。

据我所知,您无法从
UIImage
对象获取图像文件的名称。如果确实要执行此操作,可以将名称与图像一起存储到
NSDictionary
对象中:

NSArray * imageNames = [NSArray arrayWithObjects:@"A.png", @"B.png", @"C.png", nil];
NSMutableArray * array = [NSMutableArray arrayWithCapacity:[imageNames count]];
for (NSString * imageName in imageNames)
  [array addObject:[NSDictionary dictionaryWithObjectsAndKeys:
                    [UIImage imageNamed:imageName], @"image", imageName, @"name", nil]];
然后您可以像这样记录它:

NSLog(@"name = %@", [[array objectAtIndex:1] valueForKey:@"name"]);
如果用连接的图像的名称覆盖数组的“description”方法,它就可以正常工作

在这种情况下,NSLog的工作原理是向每个对象请求一个描述其自身的字符串,并向对象发送-description方法。(注意:如果一个对象没有覆盖description方法,那么您将从NSObject继承-description实现,这类似于

注意:描述方法只能用于调试目的


注意。

代码中的代码有什么作用,这是从数组中获取对象的方式。除了没有使用正确的方法,也没有使用
Objectatindex
。谢谢你的否决票。:)也许你在寻找答案之前必须正常指定问题,因为短语“
NSArray
withimages”并没有确切说明您在当前的
NSArray
中存储的是什么类型的对象。这与holex有什么关系。据他说,它可以是UIImage或UIImageView。在这两种情况下,您都无法获得图像的名称。是的,还有“图像”这个词“还表示其他任何内容,例如带有图像路径的字符串。问题是“我需要
NSLog
names”而不是他在代码片段中记录的唯一姓名。这个问题不太明显。以下是询问者真实问题的详细答案: