How to fix '-[__NSArrayM totalLength]: unrecognized - ios

I'm setting up
appDele.mMediaManager=[[BluzManager alloc] initWithConnector:appDele.mBluzConnector];
and Third-party library methods are also implemented. When I init object then the problem goes:
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSArrayM totalLength]: unrecognized
Is the NSArray about _m_arrBT goes wrong?
-(void)connectedPeripheral:(CBPeripheral *)peripheral{
if (m_dictConnect != nil) {
BOOL found = NO;
for (int i = 0; i < _m_arrBT.count; i++) {
NSMutableDictionary *dict = [_m_arrBT objectAtIndex:i];
CBPeripheral *device = [dict objectForKey:#"peripheral"];
CBPeripheral *connected = [m_dictConnect objectForKey:#"peripheral"];
// if (device.UUID == connected.UUID) {
if ([self isPeripheral:device equalPeripheral:connected]) {
found = YES;
break;
}
}
if (!found) {
[_m_arrBT addObject:m_dictConnect];
}
_m_arrBT = [self sortDeviceArray:_m_arrBT];
} else {
for (NSDictionary *dict in _m_arrBT) {
CBPeripheral *device = [dict objectForKey:#"peripheral"];
// if (device.UUID == peripheral.UUID) {
if ([self isPeripheral:device equalPeripheral:peripheral]) {
NSLog(#"devicename=%#",[dict objectForKey:#"name"]);
}
}
}
appDele.mMediaManager=[[BluzManager alloc] initWithConnector:appDele.mBluzConnector];
appDele.globalManager=[appDele.mMediaManager getGlobalManager:self];
mUserDiconnected = NO;
m_bConnect=YES;
_m_arrBT = [self sortDeviceArray:_m_arrBT];
[self.tableView reloadData];
if (!mManagerReady || !mHotplugCardArrived || !mHotplugUhostArrived || !mHotplugUSBArrived) {
// [self managerReady];
}
}

Related

CTTelephonyNetworkInfo.serviceCurrentRadioAccessTechnology changed in iOS 14 crash

if (#available(iOS 12.0, *)) {
CTTelephonyNetworkInfo * tmp = [[CTTelephonyNetworkInfo alloc] init];
if ([tmp respondsToSelector:#selector(serviceCurrentRadioAccessTechnology)]) {
[tmp.serviceCurrentRadioAccessTechnology enumerateKeysAndObjectsUsingBlock:^(NSString * _Nonnull key, NSString * _Nonnull obj, BOOL * _Nonnull stop) {
currentRadioAccessTechnology = obj;
*stop = YES;
}];
}
tmp = nil;
}
before iOS14 currentRadioAccessTechnology is NSString and
iOS14 currentRadioAccessTechnology is NSArray
if (currentRadioAccessTechnology)
{
if ([currentRadioAccessTechnology isEqualToString:CTRadioAccessTechnologyLTE])
{
returnValue = network_4g;
}
else if ([currentRadioAccessTechnology isEqualToString:CTRadioAccessTechnologyEdge]
|| [currentRadioAccessTechnology isEqualToString:CTRadioAccessTechnologyGPRS])
{
returnValue = network_2g;
}
else
{
returnValue = network_3g;
}
return returnValue;
}
if ([currentRadioAccessTechnology isEqualToString:CTRadioAccessTechnologyLTE]) crashed error info [__NSArrayM isEqualToString:]: unrecognized selector sent to instance

when will a NSObject's property could be null

I have a model translator. It reads AModel's properties, and copy the value to BModel's same property if BModel has. Now I have a crash report shows the property is null. That is so strange. The property is got from a property list and it is null.
Here is the message:
*** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<XXXXXX 0x1594051a0> valueForUndefinedKey:]: this class is not key value coding-compliant for the key (null).
+ (instancetype)convertSourceObject:(id)sourceObject {
if (!sourceObject) {
return nil;
}
id destination_object = [[self alloc] init];
uint destination_properties_count = 0;
objc_property_t *destination_properties = class_copyPropertyList([self class], &destination_properties_count);
for (int i = 0; i < destination_properties_count; i++) {
objc_property_t destination_property = destination_properties[i];
uint source_attributes_count = 0, destination_attributes_count = 0;
objc_property_attribute_t *destination_attributes = property_copyAttributeList(destination_property, &destination_attributes_count);
const char *property_char_name = property_getName(destination_property);
NSString *property_name = [NSString stringWithUTF8String:property_char_name];
objc_property_t source_property = class_getProperty([sourceObject class], property_char_name);
if (source_property && ![ignorePropertyNames() containsObject:property_name]) {
objc_property_attribute_t *source_attributes = property_copyAttributeList(source_property, &source_attributes_count);
NSString *source_ivar_type = #"";
NSString *destination_ivar_type = #"";
for (int i = 0; i < source_attributes_count; i++) {
if (strcmp(source_attributes[i].name, "T") == 0) {
source_ivar_type = [[NSString stringWithUTF8String:source_attributes[i].value] stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:#"#\""]];
break;
}
}
for (int i = 0; i < destination_attributes_count; i++) {
if (strcmp(destination_attributes[i].name, "T") == 0) {
destination_ivar_type = [[NSString stringWithUTF8String:destination_attributes[i].value] stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:#"#\""]];
break;
}
}
if ([self isPropertySetType:source_ivar_type]) {
id source_value = [sourceObject valueForKey:property_name];
if ([source_value isKindOfClass:[NSArray class]]) {
NSArray *destination_array = [self arrayConvert:(NSArray*)source_value];
if (destination_array) {
[destination_object setValue:destination_array forKey:property_name];
}
} else if ([source_value isKindOfClass:[NSDictionary class]]) {
NSDictionary *destination_dict = [self dictionaryConvert:(NSDictionary *)source_value];
if (destination_dict) {
[destination_object setValue:destination_dict forKey:property_name];
}
}
} else {
if ([destination_ivar_type isEqualToString:source_ivar_type]) {
id source_value = [sourceObject valueForKey:property_name];
if (source_value) {
[destination_object setValue:source_value forKey:property_name];
}
} else {
id source_value = [sourceObject valueForKey:property_name];
id destination_value = [NSClassFromString(destination_ivar_type) convertSourceObject:source_value];
if (destination_value) {
[destination_object setValue:destination_value forKey:property_name];
}
}
}
free(source_attributes);
} else {
continue;
}
free(destination_attributes);
}
free(destination_properties);
return destination_object;
}
The error means: you try to read a property which name's "null" AModel's property , but it doesn't exit in AModel.
You should overwrite valueForUndefinedKey in your Model Class, debug the UndefinedKey.
Check your code.It seems happened at
id source_value = [sourceObject valueForKey:property_name];
NSLog debug the property_name, see what you got.

iOS - [Class classMethod] results in unrecognized selector sent

Due to code privacy, I have to omit part of info. Just excuse me.
In TokenRequestManager.m, there is an instance method serialQueue4TokenType:
- (dispatch_queue_t)serialQueue4TokenType:(TokenType)tokenType
{
static NSMutableDictionary *serialQueueDict = nil;
if (serialQueueDict == nil) {
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
serialQueueDict = [[NSMutableDictionary alloc] initWithCapacity:TokenTypeCount];
});
}
dispatch_queue_t queue;
#synchronized (self) {
NSString *key = [TokenManager tokenName4Type:tokenType];
key = [key stringByAppendingString:NSStringFromClass([self class])];
NSValue *value = serialQueueDict[key];
if (value == nil) {
queue = dispatch_queue_create(key.UTF8String, DISPATCH_QUEUE_SERIAL);
value = [NSValue valueWithBytes:&queue objCType:#encode(dispatch_queue_t)];
serialQueueDict[key] = value;
} else {
[value getValue:&queue];
}
}
return queue;
}
Just below the #synchronized (self) { line, it calls the Class method:
+ (NSString *)tokenName4Type:(TokenType)tokenType
{
NSString *string = nil;
switch (tokenType) {
case TokenTypeBiz:
return #"TokenTypeBiz";
break;
case TokenTypeWeb:
default:
string = #"TokenTypeWeb";
break;
}
return string;
}
The code just stands there, but sometimes (a few times) the app will crash at this line with unrecognized selector sent, no matter debug or release.
Any tips will be much appreciated. Thanks.

Memory leak in ios cordova contacts plugin?

Here's the source code for the method that appears to be causing the leak.
- (void)search:(CDVInvokedUrlCommand*)command
{
NSString* callbackId = command.callbackId;
NSArray* fields = [command argumentAtIndex:0];
NSDictionary* findOptions = [command argumentAtIndex:1 withDefault:[NSNull null]];
[self.commandDelegate runInBackground:^{
// from Apple: Important You must ensure that an instance of ABAddressBookRef is used by only one thread.
// which is why address book is created within the dispatch queue.
// more details here: http: //blog.byadrian.net/2012/05/05/ios-addressbook-framework-and-gcd/
CDVAddressBookHelper* abHelper = [[CDVAddressBookHelper alloc] init];
CDVContacts* __weak weakSelf = self; // play it safe to avoid retain cycles
// it gets uglier, block within block.....
[abHelper createAddressBook: ^(ABAddressBookRef addrBook, CDVAddressBookAccessError* errCode) {
if (addrBook == NULL) {
// permission was denied or other error - return error
CDVPluginResult* result = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageToErrorObject:errCode ? (int)errCode.errorCode:UNKNOWN_ERROR];
[weakSelf.commandDelegate sendPluginResult:result callbackId:callbackId];
return;
}
NSArray* foundRecords = nil;
// get the findOptions values
BOOL multiple = NO; // default is false
NSString* filter = nil;
NSArray* desiredFields = nil;
if (![findOptions isKindOfClass:[NSNull class]]) {
id value = nil;
filter = (NSString*)[findOptions objectForKey:#"filter"];
value = [findOptions objectForKey:#"multiple"];
if ([value isKindOfClass:[NSNumber class]]) {
// multiple is a boolean that will come through as an NSNumber
multiple = [(NSNumber*)value boolValue];
// NSLog(#"multiple is: %d", multiple);
}
desiredFields = [findOptions objectForKey:#"desiredFields"];
// return all fields if desired fields are not explicitly defined
if (desiredFields == nil || desiredFields.count == 0) {
desiredFields = [NSArray arrayWithObjects:#"*", nil];
}
}
NSDictionary* searchFields = [[CDVContact class] calcReturnFields:fields];
NSDictionary* returnFields = [[CDVContact class] calcReturnFields:desiredFields];
NSMutableArray* matches = nil;
if (!filter || [filter isEqualToString:#""]) {
// get all records
foundRecords = (__bridge_transfer NSArray*)ABAddressBookCopyArrayOfAllPeople(addrBook);
if (foundRecords && ([foundRecords count] > 0)) {
// create Contacts and put into matches array
// doesn't make sense to ask for all records when multiple == NO but better check
int xferCount = multiple == YES ? (int)[foundRecords count] : 1;
matches = [NSMutableArray arrayWithCapacity:xferCount];
for (int k = 0; k < xferCount; k++) {
CDVContact* xferContact = [[CDVContact alloc] initFromABRecord:(__bridge ABRecordRef)[foundRecords objectAtIndex:k]];
[matches addObject:xferContact];
xferContact = nil;
}
}
} else {
foundRecords = (__bridge_transfer NSArray*)ABAddressBookCopyArrayOfAllPeople(addrBook);
matches = [NSMutableArray arrayWithCapacity:1];
BOOL bFound = NO;
int testCount = (int)[foundRecords count];
for (int j = 0; j < testCount; j++) {
CDVContact* testContact = [[CDVContact alloc] initFromABRecord:(__bridge ABRecordRef)[foundRecords objectAtIndex:j]];
if (testContact) {
bFound = [testContact foundValue:filter inFields:searchFields];
if (bFound) {
[matches addObject:testContact];
}
testContact = nil;
}
}
}
NSMutableArray* returnContacts = [NSMutableArray arrayWithCapacity:1];
if ((matches != nil) && ([matches count] > 0)) {
// convert to JS Contacts format and return in callback
// - returnFields determines what properties to return
#autoreleasepool {
int count = multiple == YES ? (int)[matches count] : 1;
for (int i = 0; i < count; i++) {
CDVContact* newContact = [matches objectAtIndex:i];
NSDictionary* aContact = [newContact toDictionary:returnFields];
[returnContacts addObject:aContact];
}
}
}
// return found contacts (array is empty if no contacts found)
CDVPluginResult* result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsArray:returnContacts];
[weakSelf.commandDelegate sendPluginResult:result callbackId:callbackId];
// NSLog(#"findCallback string: %#", jsString);
if (addrBook) {
CFRelease(addrBook);
}
}];
}]; // end of workQueue block
return;
}
The specific line that is doing most of the leaking is foundRecords = (__bridge_transfer NSArray*)ABAddressBookCopyArrayOfAllPeople(addrBook);, but this is confusing, given that the correct __bridge_transfer call is used. What's going on here?

Error on Adding to NSMutableArray

The following code is in my implementation file:
NSMutableArray *courseArray;
- (IBAction)btnClick:(id)sender
{
NSDictionary *courseNames;
if(![_txtBox.text isEqual:#""]) //if not empty
{
courseNames = [self retrieveCourseNamesForSemester:_txtBox.text];
for (NSString *key in courseNames)
{
NSString *val = [NSString stringWithFormat:#"%#-%#",key,[courseNames objectForKey:key]];
_txtView.text = val;
#try
{
[courseArray addObject:val];
}
#catch(NSException *e)
{
NSLog(#"Exception: %# for value = %#", e, val);
}
}
}
[_coursePicker reloadAllComponents];
_coursePicker.hidden=false;
[_txtBox resignFirstResponder];
}
Where you see the call to NSLog(), I get the following error message:
2014-03-29 00:02:25.830 WebServiceTest[44646:60b] Exception: -[__NSArrayI addObject:]: unrecognized selector sent to instance 0x8d82c30 for value = 73-522-Course Name
EDIT: Also, courseArray is populated with sample data in viewDidLoad:
- (void)viewDidLoad
{
[super viewDidLoad];
courseArray = #[#"Australia (AUD)", #"China (CNY)",
#"France (EUR)", #"Great Britain (GBP)", #"Japan (JPY)"];
}
Is there somewhere I should be defining that courseArray will take NSString objects?
The code in viewDidLoad creates an immutable array. You need to make a mutable copy, like this
(void)viewDidLoad
{
[super viewDidLoad];
courseArray = [#[#"(AUD)", #"(CNY)", #"(EUR)"] mutableCopy];
}
Try this code,
for (NSString *key in courseNames)
{
NSString *val = [NSString stringWithFormat:#"%#-%#",key,[courseNames objectForKey:key]];
_txtView.text = val;
if ([CourseArray count]==0)
{
CourseArray= [NSMutableArray arrayWithObject:val];
}
else
{
[CourseArray addObject:val];
}
}

Resources