0

hope all are having a good day. I had implemented a device check files base on something I ran into here in stack overflow, i haven't been able to find the original post to past it here. If i happen to come across, I will reference it.

This used to work great on the previous xcode 4.x but for xcode 5 I get this error.

Expected method to read dictionary element not found on object of type 'NSDictionary *' NSString *deviceName = commonNamesDictionary[machineName];

I manage to find this but it was for iOS 5 to iOS6 How to enable the new Objective-C object literals on iOS?

The code implemented is as follows. Obviously this was until iphone 5 not sure how to add the new phone archs.

.h implementation

#import <Foundation/Foundation.h>
#import <sys/utsname.h> //to check if its a iphone 4s or iphone 5
@interface deviceCheck : NSObject

+(NSString*)deviceModelName;

@end

.m implementation

#import "deviceCheck.h"
@implementation deviceCheck

* device checking method to tell the difference between devices */
+(NSString*)deviceModelName
{
struct utsname systemInfo;
uname(&systemInfo);
NSString *machineName = [NSString stringWithCString:systemInfo.machine encoding:NSUTF8StringEncoding];
NSDictionary *commonNamesDictionary =
@{
  @"i386":     @"iPhone Simulator",
  @"x86_64":   @"iPad Simulator",

  @"iPhone1,1":    @"iPhone",
  @"iPhone1,2":    @"iPhone 3G",
  @"iPhone2,1":    @"iPhone 3GS",
  @"iPhone3,1":    @"iPhone 4",
  @"iPhone4,1":    @"iPhone 4S",
  @"iPhone5,1":    @"iPhone 5(GSM)",
  @"iPhone5,2":    @"iPhone 5(GSM+CDMA)",

  @"iPad1,1":  @"iPad",
  @"iPad2,1":  @"iPad 2(WiFi)",
  @"iPad2,2":  @"iPad 2(GSM)",
  @"iPad2,3":  @"iPad 2(CDMA)",
  @"iPad2,4":  @"iPad 2(WiFi Rev A)",
  @"iPad2,5":  @"iPad Mini(WiFi)",
  @"iPad2,6":  @"iPad Mini(GSM)",
  @"iPad2,7":  @"iPad Mini(GSM+CDMA)",
  @"iPad3,1":  @"iPad 3(WiFi)",
  @"iPad3,2":  @"iPad 3(GSM+CDMA)",
  @"iPad3,3":  @"iPad 3(GSM)",
  @"iPad3,4":  @"iPad 4(WiFi)",
  @"iPad3,5":  @"iPad 4(GSM)",
  @"iPad3,6":  @"iPad 4(GSM+CDMA)",

  @"iPod1,1":  @"iPod 1st Gen",
  @"iPod2,1":  @"iPod 2nd Gen",
  @"iPod3,1":  @"iPod 3rd Gen",
  @"iPod4,1":  @"iPod 4th Gen",
  @"iPod5,1":  @"iPod 5th Gen",

  };
NSString *deviceName = commonNamesDictionary[machineName]; // <-- here is the issue
if (deviceName == nil) {
    deviceName = machineName;
}
return deviceName;

}
@end

Anyone else has come across this issue?

In advance thanks for your help on this.

Community
  • 1
  • 1
Jiraheta
  • 471
  • 7
  • 17
  • 1
    What happens if you use the good old `NSDictionary` methods like `dictionaryWithObjectsAndKeys:` and `objectForKey:` instead of literals and/or subscript operators? – Macmade Nov 19 '13 at 19:35
  • I ran this code in an Xcode 5.0.2 project targeting iOS 7 under ARC with no errors. So more information is needed. Consider creating a new Xcode project with just this code in `didFinishLaunchingWithOptions`. – zaph Nov 19 '13 at 20:43
  • Actually you are right, the code works by itself but in my project it gives me that error. I am trying to figure out why is it that this is happenning. As i mention before this is something I manage to find in a post here some time back. – Jiraheta Nov 19 '13 at 20:52
  • I trid @Macmade suggestion and it looks like the error seems to have gone away. [link](http://stackoverflow.com/questions/20080282/ios-device-check-for-xcode-5?noredirect=1#comment29914984_20080282) – Jiraheta Nov 19 '13 at 20:54

2 Answers2

3

The issue was very simple... yet, took me a while to figure it out. Hope this helps anyone else that comes across this issue in the future. For some reason when I was importing Sys/utsname.h it was referring to the copy of IOS5 and not the one from IOS7, I just had to delete the import close xcode and re-declare the import (not sure if that was what did the trick, but those are the steps I had to follow for this to finally worked). How can you check if its point on the right one just "Command + click" and it will take you to where is referencing from.

I took the opportunity and updated all the new devices that have come out.

@{
  @"i386":     @"iPhone Simulator",
  @"x86_64":   @"iPad Simulator",

  //IPHONE
  @"iPhone1,1":    @"iPhone",
  @"iPhone1,2":    @"iPhone 3G",
  @"iPhone2,1":    @"iPhone 3GS",
  @"iPhone3,1":    @"iPhone 4",
  @"iPhone4,1":    @"iPhone 4S",
  @"iPhone5,1":    @"iPhone 5(GSM)",
  @"iPhone5,2":    @"iPhone 5(GSM+CDMA)",
  @"iPhone5,3":    @"iPhone 5c(GSM)",
  @"iPhone5,4":    @"iPhone 5c(Global)",
  @"iPhone6,1":    @"iPhone 5s(GSM)",
  @"iPhone6,2":    @"iPhone 5s(Global)",

  //IPAD
  @"iPad1,1":  @"iPad",
  @"iPad2,1":  @"iPad 2(WiFi)",
  @"iPad2,2":  @"iPad 2(GSM)",
  @"iPad2,3":  @"iPad 2(CDMA)",
  @"iPad2,4":  @"iPad 2(WiFi Rev A)",
  @"iPad2,5":  @"iPad Mini(WiFi)",
  @"iPad2,6":  @"iPad Mini(GSM)",
  @"iPad2,7":  @"iPad Mini(GSM+CDMA)",
  @"iPad3,1":  @"iPad 3(WiFi)",
  @"iPad3,2":  @"iPad 3(GSM+CDMA)",
  @"iPad3,3":  @"iPad 3(GSM)",
  @"iPad3,4":  @"iPad 4(WiFi)",
  @"iPad3,5":  @"iPad 4(GSM)",
  @"iPad3,6":  @"iPad 4(GSM+CDMA)",
  @"iPad4,1":  @"iPad Air(WiFi)",
  @"iPad4,2":  @"iPad Air(Cellular)",
  @"iPad4,4":  @"iPad Mini 2G(WiFi)",
  @"iPad4,5":  @"iPad Mini 2G(Cellular)",

  //IPOD
  @"iPod1,1":  @"iPod 1st Gen",
  @"iPod2,1":  @"iPod 2nd Gen",
  @"iPod3,1":  @"iPod 3rd Gen",
  @"iPod4,1":  @"iPod 4th Gen",
  @"iPod5,1":  @"iPod 5th Gen",

  };
Jiraheta
  • 471
  • 7
  • 17
0

I try the following and it seems that it working and got rid of the error. But still not sure why this happened.

What happens if you use the good old NSDictionary methods like dictionaryWithObjectsAndKeys: and objectForKey: instead of literals and/or subscript operators? – Macmade

the changes to the code as follow.

NSArray *keys = [NSArray arrayWithObjects:@"i386","x86_64",@"iPhone1,1",@"iPhone1,2",@"iPhone2,1",@"iPhone3,1",@"iPhone4,1",@"iPhone5,1",@"iPhone5,2",@"iPad1,1",@"iPad2,1",@"iPad2,2",@"iPad2,3",@"iPad2,4",@"iPad2,5",@"iPad2,6",@"iPad2,7",@"iPad3,1",@"iPad3,2",@"iPad3,3",@"iPad3,4",@"iPad3,5",@"iPad3,6",@"iPod1,1",@"iPod2,1",@"iPod3,1",@"iPod4,1",@"iPod5,1", nil];
NSArray *objects = [NSArray arrayWithObjects:@"iPhone Simulator",@"iPad Simulator",@"iPhone",@"iPhone 3G",@"iPhone 3GS",@"iPhone 4",@"iPhone 4S",@"iPhone 5(GSM)",@"iPhone 5(GSM+CDMA)",@"iPad",@"iPad 2(WiFi)",@"iPad 2(GSM)",@"iPad 2(CDMA)",@"iPad 2(WiFi Rev A)",@"iPad Mini(WiFi)",@"iPad Mini(GSM)",@"iPad Mini(GSM+CDMA)",@"iPad 3(WiFi)",@"iPad 3(GSM+CDMA)",@"iPad 3(GSM)",@"iPad 4(WiFi)",@"iPad 4(GSM)",@"iPad 4(GSM+CDMA)",@"iPod 1st Gen",@"iPod 2nd Gen",@"iPod 3rd Gen",@"iPod 4th Gen",@"iPod 5th Gen",nil];
NSDictionary *commonNamesDictionary_ = [NSDictionary dictionaryWithObjects:objects forKeys:keys];

NOTE: I notice that if I comment the code replace it with the above one and then switch it back to the original code and comment the above code, xcode shuts up and continues as normal. Not sure whats its going on with xcode. =/ Hope this helps someone out there or someone can share some light and explain what is happening.

Jiraheta
  • 471
  • 7
  • 17