About NSArray nil crash error - ios

How to avoid this crash error!Crash in main thread Log is
ProcessArray:()
ProcessArray == 0
-[NSNull length]: unrecognized selector sent to instance 0x3c4e1090
my code:
ProcessArray = [EventSheetDetailArray valueForKey:#"Process"];
NSLog(#"ProcessArray:%#",ProcessArray);
if (ProcessArray.count > 0 ) {
NSLog(#"ProcessArray != 0");
[self ProcessJSONDateFormat];
}else{
NSLog(#"ProcessArray == 0");
}
I change judge below!
id value = [EventSheetDetailArray valueForKey:#"Process"];
if ( ![[NSNull null] isEqual:value] )
{
ProcessArray = value;
NSLog(#"1111111");
}else{
NSLog(#"2222222");
}
But always run NSLog(#"1111111") this line!
Regardless of whether any value!

Validate the return value from [EventSheetDetailArray valueForKey:#"Process"]
id value = [EventSheetDetailArray valueForKey:#"Process"];
if ( ![[NSNull null] isEqual:value] )
{
ProcessArray = value;
}
OR
id value = [EventSheetDetailArray valueForKey:#"Process"];
if ( [value isKindOfClass:[NSArray class]] )
{
ProcessArray = value;
}
then you can call NSArray member functions, don't just assume it is NSArray..

The answer is actually quite simple.
if([ProcessArray count] != [NSNULL NULL]) {
if (ProcessArray.count > 0 ) {
NSLog(#"ProcessArray != 0");
[self ProcessJSONDateFormat];
}else {
NSLog(#"ProcessArray == 0");
}
}else {
NSLog(#"NULL...insert some value");
}

Sorry!I found the cause of the error,Because my other code have error, That caused crash main thread -[NSNull length] reason!

If your array is empty then you can't check for count otherwise it will crash.I faced the same issue.Just use "if(!(array==nil))".This is true when your array is empty otherwise it will move to else part.

Related

swift: EXC_BAD_ACCESS with using Realm and FolioReaderKit

I installed FolioReaderKit using Cocoapods to read epub book. I'm trying to highlight text and add it to a list of highlights. But when I do this I get this error and my app crashes:
Thread 1: EXC_BAD_ACCESS (code=257, address=0x41c4a1c96ae55d46)
line:
value = RLMGetOptional(static_cast<RLMOptionalBase *>(object_getIvar(obj, prop.swiftIvar)));
in RLMAccessor.mm:
id RLMAccessorContext::propertyValue(__unsafe_unretained id const obj, size_t propIndex,
__unsafe_unretained RLMProperty *const prop) {
// Property value from an NSArray
if ([obj respondsToSelector:#selector(objectAtIndex:)]) {
return propIndex < [obj count] ? [obj objectAtIndex:propIndex] : nil;
}
// Property value from an NSDictionary
if ([obj respondsToSelector:#selector(objectForKey:)]) {
return [obj objectForKey:prop.name];
}
// Property value from an instance of this object type
id value;
if ([obj isKindOfClass:_info.rlmObjectSchema.objectClass] && prop.swiftIvar) {
if (prop.array) {
return static_cast<RLMListBase *>(object_getIvar(obj, prop.swiftIvar))._rlmArray;
}
else if (prop.swiftIvar == RLMDummySwiftIvar) {
// FIXME: An invalid property which we're pretending is nil until 4.0
// https://github.com/realm/realm-cocoa/issues/5784
return NSNull.null;
}
else { // optional
value = RLMGetOptional(static_cast<RLMOptionalBase *>(object_getIvar(obj, prop.swiftIvar)));
}
}
else {
// Property value from some object that's KVC-compatible
value = RLMValidatedValueForProperty(obj, [obj respondsToSelector:prop.getterSel] ? prop.getterName : prop.name,
_info.rlmObjectSchema.className);
}
return value ?: NSNull.null;
}
How to fix it?

Application crash while accessing data element of linked list when data is NULL

I am using following code to traverse through linked list in objective c
const MSList *calls = linphone_core_get_calls(LC);
if (calls == NULL)
{
[self dismissCtrl];
//how to check current is which screen is on
// while ((currentView == CallView.compositeViewDescription) ||
// (currentView == CallIncomingView.compositeViewDescription) ||
// (currentView == CallOutgoingView.compositeViewDescription)) {
// [self popCurrentView];
// }
} else {
linphone_call_resume((LinphoneCall *)calls->data);
while (calls)
{
if(calls->data != NULL && calls->data != nil && calls->data != (__bridge void *)((id)[NSNull null]))
{
//crash
if (linphone_call_get_state((LinphoneCall *)calls->data) == LinphoneCallIncomingReceived ||
linphone_call_get_state((LinphoneCall *)calls->data) == LinphoneCallIncomingEarlyMedia) {
[self displayIncomingCall:(LinphoneCall *)calls->data];
break;
}
}
calls = calls->next;
}
Application is crashing when entire list is not null but its data,previous or next value is NULL. I have added code to check if data is NULL but if it(data) is NULL, then i will not be able to access it and in condition itself application is crashing. How to prevent this ? I have attached screen-shot for where application is crashing and what is the value that list contains at that time.
if (calls == NULL) {
[self dismissCtrl];
//how to check current is which screen is on
// while ((currentView == CallView.compositeViewDescription) ||
// (currentView == CallIncomingView.compositeViewDescription) ||
// (currentView == CallOutgoingView.compositeViewDescription)) {
// [self popCurrentView];
// }
}
else
{
size_t count = bctbx_list_size(calls);
linphone_call_resume((LinphoneCall *)calls->data);
int i = 0;
while (calls )
{
if(i < count)
{
if ( calls->data == (__bridge void *)((id)[NSNull null]) || calls->data == NULL || calls->data == nil )
{
return;
}
LinphoneCall *objCall = (LinphoneCall *)calls->data;
LinphoneCallState state = (objCall != NULL) ? linphone_call_get_state(call) : 0;
if (state == LinphoneCallIncomingReceived ||
state == LinphoneCallIncomingEarlyMedia)
{
[self displayIncomingCall:(LinphoneCall *)calls->data];
break;
}
//calls ? calls->data : NULL
// calls = calls ? calls->next : NULL;
calls = calls->next;
i = i+1;
}
else
{
break;
}
}

How to check if an object is nil in Objective-C? [duplicate]

This question already has answers here:
Testing for nil in Objective-C -- if(x != nil) vs if(x)
(4 answers)
Closed 7 years ago.
So I know this seems pretty basic but it doesn't seem to be working for some reason. I have the following code.
Target *t = self.skill.target;
if (![t isEqual:nil]) {
NSLog(#"Not nil");
}
I tried this and it comes up as not nil everytime which is great except for when t should actually be nil. I even tried putting variation into the code like so and my t is still coming up as not nil for some reason. Am I doing something wrong? :\
Target *t = self.skill.target;
t = nil;
if (![t isEqual:nil]) {
NSLog(#"Not nil");
}
You can do it normally just by checking the object itself. There is also a good explanation on NSHipster for NSNull.
if( myObject ){
// do something if object isn't nil
} else {
// initialize object and do something
}
otherwise just use
if( myObject == nil ){
}
Target *t = self.skill.target;
t = nil;
if (t) {
NSLog(#"t is Not nil");
}
if (!t) {
NSLog(#"t is nil");
}
As this answer says:
Any message to nil will return a result which is the equivalent to 0 for the type requested. Since the 0 for a boolean is NO, that is the result.
So, a nil object is special. you can't compare it using the isEqual method. you should compare it without sending it a message, like this:
if (t)
{
}
or simply:
if (t != nil)
{
}
The if operation checks nillable so this should works:
Target *t = self.skill.target;
if (t) {
NSLog(#"Not nil");
}else{
NSLog(#"nil");
}
Hope it helps.

how to check if bond array is Null or empty in iphone sdk?

I want to check if my array is empty or null and not null or not empty. Below is some sample code I have written. Can you tell me if this is on the right track?
/// first time get data there
{
Bonds = Null;
User = {
DOB = "12/09/1988";
about = "test about";
city = CA;
};
success = True;
}
////////Second time get data there
{
Bonds = (
{
DOB = "12/09/1988";
about = "";
city = CA;
},
{
DOB = "12/09/1988";
about = "";
city = CA;
}
);
User = {
DOB = "12/09/1988";
about = about;
city = CA;
};
success = True;}
#try
{
if([Your_array objectAtIndex:int]== (id)[NSNull null] || [Your_array objectAtIndex:int]==0)
{
// if index get null
}
else
{
//your code if array index is not null
}
}
#catch (NSException * e)
{
NSLog(#"NSException");
}
If you are asking how to check for an empty array in Objective C (you mentioned iPhone SDK but not the language- Swift or Objective C) you can simply check the count.
if ([myArray count]==0) // the array is empty
{
...
}
try this
if(![Bonds isKindOfClass:[NSArray Class]]) // for non nil array and
if([Bounds firstObject]) //to check if array having at least one element
if array is not nil, and if not - check if it is not empty.
if (!array || !array.count){
......
}
try this
if(array && ![array isEqual:[NSNull null]] && [array count] > 0)
{
}
You can check like this
if ((NSNull*) Bonds !=[NSNull null] && Bonds!=nil && [array count] > 0)
{
}
if (!array || !array.count){
......
}

if(objc_getAssociatedObject(self, &Key)) return true for nil object

How can I test that objc_getAssociatedObject is nil? The following says element is not nil but the associated object has never been set before nor been accessed.
static char orderedElementKey = 11;
if (objc_getAssociatedObject(self, &orderedElementKey) != nil)
{
NSLog(#"element is not nil");
return objc_getAssociatedObject(self, &orderedElementKey);
}
NSLog(#"elelement was nil !");
static char orderedElementKey = 11;
//objc_setAssociatedObject(self, &orderedElementKey, #"value", OBJC_ASSOCIATION_RETAIN);
if (objc_getAssociatedObject(self, &orderedElementKey) != nil)
{
NSLog(#"Element: %#", objc_getAssociatedObject(self, &orderedElementKey));
}
else
{
NSLog(#"element nil !");
}
prints "Element nil !" while removing the comment on objc_setAssociatedObject() prints "Element: value". How is your code different?

Resources