且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

数组中的UIImage名称不断返回nil值.

更新时间:2023-11-29 08:17:28

更改UIImageView的图像时出现了问题.尝试todayShirt.image = UIImage(imageLiteral: "\(shirtName)")todayPants.image = UIImage(imageLiteral: "\(pantsName)").

Something is going wrong when you change the image of the UIImageView. Try todayShirt.image = UIImage(imageLiteral: "\(shirtName)") and todayPants.image = UIImage(imageLiteral: "\(pantsName)").

然后非常确定,您已在XCode项目中插入了4张图像:shirt1.pngshirt2.pngpants1.pngpants2.png.如果它们是shirt1.jpg等,也可以.

Then be very very sure that you have 4 images inserted in your XCode project: shirt1.png, shirt2.png, pants1.png, and pants2.png. It's also ok if they are shirt1.jpg, etc.

如果仍然不能解决问题,请尝试更改数组创建方法.只需使用创建数组&而不是使用NSArray并仅在viewdidload()部分中向其添加对象即可.在全局范围内添加对象.因此,现在在具有var shirtImage: NSArrayvar shirtImage: NSArray的位置,将它们都删除,并添加var shirtsArray:NSMutableArray = ["shirt1", "shirt2"]var pantsArray:NSMutableArray = ["pants1", "pants2"].

If this still isn't helping, try changing your array-creation method. Instead of using NSArray and only adding objects to it in the viewdidload() section, just create the arrays & add objects in the global scope. So, right now where you have var shirtImage: NSArray and var shirtImage: NSArray, remove both of those and add var shirtsArray:NSMutableArray = ["shirt1", "shirt2"] and var pantsArray:NSMutableArray = ["pants1", "pants2"].