Shopify iOS SDK - issue converting BuyProductVariant to BuyProduct - ios

I am having an issue converting a BuyProductVariant to BuyProduct, as from BuyCart i am getting the array of all the Variants which are added in that cart with the following code
NSArray *arr = cart.lineItems;
Now as per my understanding, this array contains all the Variants which are added in the Cart.
After that I am getting those object in BuyProductVariant
for (int j=0; j<arr.count; j++) {
BUYProductVariant *variant = arr[j];
//As the product is defined in BuyProductVariant class
BUYProduct *product = variant.product;
//But when the above line executes the app crashes, below is the description
}
exception: -[__NSDictionaryM product]: unrecognized selector sent to instance 0x7fd3286d3c50
Now, in the above code, where am i doing the wrong, any help would be very helpful to me.
Thanks

the BUYCart.lineItems array does not contain BUYProductVariant object. It contains BUYCartLineItem objects. You can retrieve your BUYProductVariant from it.
for (BUYCartLineItem* line in cart.lineItems) {
BUYProductVariant* productVariant = line.variant;
}

Related

__NSCFDictionary doesn't respond to any selectors

I am trying to Parse JSON that looks like this:
food = {
"food_id" = 4823;
servings = {
serving = (
{
calcium = 9;
calories = 221;
carbohydrate = "16.20";
cholesterol = 31;
I can successfully retrieve the array at the [food][servings][serving] level, which I confirm via a log statement has the appropriate class, __NSCFArray, but I run into a problem when iterating through that array and trying to do useful things with the contained information:
for (id foodId in resultsPerServing) {
NSLog(#"hree is a result %#", foodId);
foodObjectClass *foodObject = [foodObjectClass new];
NSDictionary *foodIdDictionary = (NSDictionary *)foodId;
if ([foodIdDictionary respondsToSelector:#selector(allKeys)]) {
[foodObject getDetailsFromResponseObject:foodIdDictionary];
} else {
unsigned int mc = 0;
Method * mlist = class_copyMethodList(object_getClass(foodIdDictionary), &mc);
NSLog(#"%d methods", mc);
for(int i=0;i<mc;i++)
NSLog(#"Method no #%d: %s", i, sel_getName(method_getName(mlist[i])));
[NSException raise:#"DIDN'T GET A DICTIONARY" format:#"here is the object %# which has class %#", resultsPerServing, [responseObject class]];
}
}
This causes the NSException to be raised without fail although in the NSException the output from printing the object looks like a dictionary. Additionally, when the exception prints out the class of the object which does not respond to allKeys, that object has class __NSCFDictionary. What gives? Why does this dictionary not have an allKeys method and how can I get a functional dictionary to work with?
Even more puzzling is that the runtime code below (copied from an SO post for which I have lost the link) indicates that the dictionary has 0 methods. Why does this object have no methods? How can I get an object that does have methods?
make a check to confirm that it as NSDictionary first?
if ([foodId isKindOfClass:[NSDictionary class]] ) {
foodIdDictionary = (NSDictionary *)foodId;
[foodObject getDetailsFromResponseObject:foodIdDictionary];
}

How To convert [__NSArrayI integerValue] to integer value?

This the line i have using to convert the object to integer values,Inside For Loop I have Placed This code
NSInteger tag=[[arrFullSubCategory valueForKey:#"category"] integerValue];
Inside arrFullSubCategory:
(
{
category = 35;
image = "images/Hatchback.jpg";
name = Hatchback;
parent = 20;
},
{
category = 36;
image = "images/Sedan.jpg";
name = Sedan;
parent = 20;
},
{
category = 37;
image = "images/SUV.jpg";
name = SUV;
parent = 20;
}
)
Exception:
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSArrayI integerValue]: unrecognized selector sent to instance 0x7ff4ba58f930'
arrFullSubCategory is an array and you should reach it's elements first. Than you will have NSDictionary objects. After that you can access your category element. So I think your code should be like that:
for (NSInteger i = 0; i < arrFullSubCategory.count; ++i) {
NSInteger tag=[[[arrFullSubCategory objectAtIndex:i] valueForKey:#"category"] integerValue];
}
Explanation of the error:
The error means you have an array, and arrays don't respond to integerValue.
Your variable arrFullSubCategory references an array (of 3 elements), and each element is a dictionary. If you call valueForKey: on an array of dictionaries then the key lookup is performed for each dictionary and an array is constructed for the results. In your case the result (using literal syntax) is the array:
#[ #35, #36, #37 ]
Whether this array is directly useful to you, or whether you should access the array one element at a time - using a loop or method which calls a block per element, etc. - will depend on what your goal is.
HTH
Try this code inside for loop I hope this help you
NSInteger tag=[[[arrFullSubCategory objectAtIndex:i] valueForKey:#"category"] integerValue];
you have array of dictionary, So you use it given below code
[[[arrFullSubCategory objectAtIndex:] objectForKey:#"category"] integerValue]

Error while parsing NSMutablearray values

I have a NSMuttableArraywhich contains the structure like below,
28 = (
twenty
);
30 = (
thirty
);
32 = (
thirty
);
I am trying to store the values like 28,30,32 into a separate array. I tried loop as,
NSMutableArray *getvalueshere;
for(int i=0;i<getvalueshere.count;i++)
NSArray *getval = [getvalueshere objectAtindex:i];
}
On trying this, my app gets crash saying __NSCFDictionary objectAtIndex:]: unrecognized selector sent to instance.
Your object is a NSDictionary as the error message reveals.
You can get the numbers – which are actually the keys of the dictionary – with
NSArray *getval = [getvalueshere allKeys];
It's saying getvalueshere is an NSDictionary which doesn't recognize objectAtIndex. When you declared getvalueshere, you gave it a NSDictionary like structure. It is considered an NSDictionary even though you explicitly stated it should be an NSMutableArray.

How for in loop works internally - Objective C - Foundation

I found this answer:
https://stackoverflow.com/a/5163334/1364174
Which presents how for in loop is implemented.
NSFastEnumerationState __enumState = {0};
id __objects[MAX_STACKBUFF_SIZE];
NSUInteger __count;
while ((__count = [myArray countByEnumeratingWithState:&__enumState objects:__objects count:MAX_STACKBUFF_SIZE]) > 0) {
for (NSUInteger i = 0; i < __count; i++) {
id obj = __objects[i];
[obj doSomething];
}
}
The problem is that, I found it wrong.
First of all, when you have Automatic Reference Counting (ARC) turned on, you got an error
Sending '__strong id *' to parameter of type '__unsafe_unretained_id*' changes retain/release properties of pointer
But even when I turn ARC off I found out that I __object array seems to behave strangely :
This is actual Code (I assumed MAX_STACKBUFF_SIZE to be 40):
#autoreleasepool {
NSArray *myArray = #[#"a", #"b", #"c", #"d", #"e", #"f", #"g"];
int MAX_STACKBUFF_SIZE = 40;
NSFastEnumerationState __enumState = {0};
id __objects[MAX_STACKBUFF_SIZE];
NSUInteger __count;
while ((__count = [myArray countByEnumeratingWithState:&__enumState objects:__objects count:MAX_STACKBUFF_SIZE]) > 0) {
for (NSUInteger i = 0; i < __count; i++) {
id obj = __objects[i];
__enumState.itemsPtr
NSLog(#" Object from __objects ! %#", obj); // on screenshot different message
}
}
}
return 0;
I got EXC_BAD_ACESS when I try to get the contents of the __object array.
I also found out that when you try to iterate through __enumState.itemsPtr it actually works.
Could you explain me what is going on here ? Why my __objects seems to be "shrunken down". And why it doesn't contains desired object? And why is that error when ARC is turned on.
Thank you very very much in advance for your time and effort! (I provided screenshot for better understanding what causes an error)
First of all, strong pointers cannot be used in C-structures, as explained in the "Transitioning to ARC Release Notes", therefore the objects array has be be declared
as
__unsafe_unretained id __objects[MAX_STACKBUFF_SIZE];
if you compile with ARC.
Now it is not obvious (to me) from the NSFastEnumeration documentation, but it is
explained in Cocoa With Love:Implementing countByEnumeratingWithState:objects:count:
that the implementation need not fill the supplied objects array, but can just set
__enumState.itemsPtr to an existing array (e.g. some internal storage). In that case, the contents of the
__objects array is undefined, which causes the crash.
Replacing
id obj = __objects[i];
by
id obj = __enumState.itemsPtr[i];
gives the expected result, which is what you observed.
Another reference can be found in the "FastEnumerationSample" sample code:
You have two choices when implementing this method:
1) Use the stack
based array provided by stackbuf. If you do this, then you must
respect the value of 'len'.
2) Return your own array of objects. If
you do this, return the full length of the array returned until you
run out of objects, then return 0. For example, a linked-array
implementation may return each array in order until you iterate
through all arrays.
In either case, state->itemsPtr MUST be a valid
array (non-nil). ...

Search NSArray and add to NSMutableArray

I have an Xcode project with two NSArrays of objects, one named completionArray filled with #"yes" and #"no" and one named allRequirements with objects that represent college classes such as #"ITCS 1215". My goal is to search completionArray until I reach #"no", then copy the object at that index from allRequirements in to suggestionArray, a NSMutableArray.
Here is my code so far, which does not work because it is throwing the error "Expected Identifier" after the last closing bracket.
for (int i = 0; i < [self.allRequirements count]; i++)
{
if([self.completionArray[i] isEqual:#"no"])
[self.suggestionArray addObject:[self.allRequirements[i]];
}
Thanks all.
Change this line:
[self.suggestionArray addObject:[self.allRequirements[i]];
to:
[self.suggestionArray addObject:self.allRequirements[i]];

Resources