Related
I am working on SQLite but when I replace the data it adds again and again to my database, basically I want to store the alarm name, alarm type and alarm tune path, there is should be only 6 rows in NotiType because I am inserting exactly the 6 elements in every column. But the problem here is when I am replacing the data when there is change in alarm names types and tunes everyday, but the data is being inserted in every column below the and counting when ever the table is calling replace data function.
if([fileManager fileExistsAtPath:_dbPath] == YES) {
const char *dbPathagain = [_dbPath UTF8String];
if(sqlite3_open(dbPathagain, &_DB) == SQLITE_OK ) {
NSLog(#"database is open");
char *errorMessage;
const char *sql_statement = "CREATE TABLE IF NOT EXISTS NotiType(Alarm TEXT, Type TEXT, TPath TEXT)";
NSLog(#"created table success");
// sqlite3_stmt *statement = NULL;
// const char *dbPathagain = [ _dbPath UTF8String];
if([fileManager fileExistsAtPath:_dbPath] == YES) {
sqlite3_stmt *statement = NULL;
const char *dbPathagain = [_dbPath UTF8String];
if(sqlite3_open(dbPathagain, &_DB) == SQLITE_OK ) {
for(int itemIndex = 0; itemIndex < [azanst count];itemIndex++){
NSString *myname = [azanst objectAtIndex:itemIndex];
NSString *mytype = [alarmsstype objectAtIndex:itemIndex];
NSString *mytunepath = [alarmtune objectAtIndex:itemIndex];
NSString *insertSQLData = [NSString stringWithFormat:#"REPLACE INTO NotiType(Alarm, Type, TPath) VALUES (\"%#\", \"%#\",\"%#\")", myname, mytype, mytunepath];
NSLog(#"here is alarm names %#", myname);
NSLog(#"here alarm types %#", mytype);
NSLog(#"here alarm tune path %#", mytunepath);
const char *insert_statement = [insertSQLData UTF8String];
sqlite3_prepare_v2(_DB, insert_statement, -1, &statement, NULL);
if (sqlite3_step(statement) == SQLITE_DONE) {
NSLog(#"data added successfully");
}
else {
NSLog(#"could not add timings");
}
}
sqlite3_finalize(statement);
sqlite3_close(_DB);
}
}
if (sqlite3_exec(_DB, sql_statement, NULL, NULL, &errorMessage) != SQLITE_OK) {
NSLog(#"failed to insert in table");
}
sqlite3_close(_DB);
}
else {
NSLog(#"failed to open db or cretate table");
NSLog(#"Database Error Message : %s", sqlite3_errmsg(_DB));
}
}
UPDATE
I am doing something like this
for (i=0, i<= ID.count, i++) {
NSString *insertSQLData = [NSString stringWithFormat:#"UPDATE NotiType SET Alarm =" + myname "," + "Type =" + mytype "," + "Tpath =" mytunepath + " where ID == i"];
}
You forgot to provide a WHERE statement :
NSString *insertSQLData = [NSString stringWithFormat:#"REPLACE INTO
NotiType(Alarm, Type, TPath) VALUES (\"%#\", \"%#\",\"%#\")", myname,
mytype, mytunepath];
you should add something like WHERE _id = %#, myAlarmUniqIdentifier to make sureyou only change the one that is beeing edited
edit : Come to think of it I also recommand using UPDATE, I'm not sure if REPLACE is what you want here..
UPDATE {table} SET {column1} = {editedObject.value1}, {column2} = {editedObject.value2} WHERE {uniqueIdField} = {editedObject.identifier};
EDIT 2 :
following your edit, update your for loop like this :
NSString *myname = [azanst objectAtIndex:i];
NSString *mytype = [alarmsstype objectAtIndex:i];
NSString *mytunepath = [alarmtune objectAtIndex:i];
NSNumber *myid = [ID objectAtIndex:i]; //<-- get the unique identifier for the item number āiā
[NSString stringWithFormat:#"UPDATE NotiType SET Alarm=%#, Type=%#, Tpath=%# where ID=%#", myname, mytype, mytunepath, myid];
I have a class that holds attributes in a dictionary where the keys are well defined. I would like to replace this attribute dictionary with a class, let's call it AttributeSet. Where there were defined keys:
extern NSString *const Foo;
I would like to have properties:
#interface AttributeSet : NSObject
#property(strong) NSString *Foo;
...a ton more
#end
I would actually like the AttributeSet object to use a dictionary behind the scenes because for backwards compatibility reasons. So when this happens:
attributeSet.Foo = #"bar";
I actually want this to happen:
- (void)setFoo:(NSString *)foo {
self.attributes[Foo] = foo; //Foo is the extern variable Foo
}
but I don't want to have to define getters and setters for all of the properties.
I know that I can use key-value observing but that will 1) require me to have a mapping of (property name) #"Foo" --> (variable name) Foo and 2) result in both the property being set and the dictionary value being set when in reality I just want the dictionary to be set.
I know that I can do something like this: https://github.com/iosptl/ios6ptl/blob/master/ch28/Person/Person/Person.m
but that would 1) still require me to have a mapping and 2) require me to have an #dynamic for every property.
Is there a more automatic way to do this?
Thanks
To use the dynamically-generated accessor approach, as illustrated in the Person code you linked, without requiring #dynamic, you can declare the properties in a category on your class rather than the class itself:
#interface AttributeSet : NSObject
// ... no properties here ...
#end
#interface AttributeSet (YourPropertiesCategoryName)
#property(strong) NSString *Foo;
...a ton more
#end
The compiler will auto-synthesize properties declared in the class itself or in a class extension (which looks like a category with no category name), but not for a category.
Note that you don't need to and shouldn't provide an implementation for the category. (If you do, the compiler will complain about the lack of implementation for the properties. It won't auto-synthesize them, but you'll still need to use #dynamic to silence the warnings.)
After a bit of time, I think I've come up with quite the extensible solution for you. All it requires of you is to simply create your objects using the following helper class, like this:
#import "DictionaryBackedObject.h"
extern NSString *const foo;
NSString *const foo = #"Foo";
#interface Foo : NSObject
#property NSString *foo;
#end
#implementation Foo
#end
int main() {
Foo *object = [DictionaryBackedObject dictionaryBackedObjectOfType:[Foo class]
backingDictionary:#{ foo: #"Bar" }
mutable:NO];
NSLog(#"%#", [object foo]);
}
Note: This implementation is far from perfect, and it does use the 'dreaded' dlsym API, meaning, that you cannot strip your symbols from the executable should you wish to use this class. Also, it may cause rejection should this be submitted to the app store. There are other ways to automatically determine the key to use along with the dictionary, however, should you wish to find a workaround.
This implementation does support struct properties, as well as weak, copy, and atomic ones as well. It will be significantly slower than setting the property on a normal object, as this goes through objective-c's forwarding API (required to support struct returns).
Hopefully this helps you out, I certainly had a lot of fun making it.
DictionaryBackedObject.h
#interface DictionaryBackedObject : NSObject
+(id) dictionaryBackedObjectOfType:(Class) kls backingDictionary:(NSDictionary *) dictionary mutable:(BOOL) isMutable;
#end
DictionaryBackedObject.m
#import "DictionaryBackedObject.h"
#include <stdalign.h>
#include <dlfcn.h>
#import ObjectiveC.runtime;
#import ObjectiveC.message;
__attribute__((noinline))
static SEL property_getGetterSelector(objc_property_t property) {
char *getter = property_copyAttributeValue(property, "G");
if (getter) {
SEL result = sel_registerName(getter);
free(getter);
return result;
}
return sel_registerName(property_getName(property));
}
__attribute__((noinline))
static SEL property_getSetterSelector(objc_property_t property) {
char *setter = property_copyAttributeValue(property, "S");
if (setter) {
SEL result = sel_registerName(setter);
free(setter);
return result;
}
char buffer[512];
char propertyName[512];
strncpy(propertyName, property_getName(property), 512);
propertyName[0] = toupper(propertyName[0]);
snprintf(buffer, 512, "set%s", propertyName);
return sel_registerName(buffer);
}
struct objc_property_attributes_t {
union {
struct {
int nonatomic : 1;
int copy : 1;
int weak : 1;
int strong : 1;
};
int memory_mode;
};
int is_readonly;
int is_dynamic;
};
static inline BOOL property_isAttributeNull(objc_property_t property, const char *attr) {
void *value = property_copyAttributeValue(property, attr);
BOOL results = value == NULL;
free(value);
return results;
}
static struct objc_property_attributes_t property_getPropertyAttributes(objc_property_t property) {
struct objc_property_attributes_t attrs;
attrs.nonatomic = !property_isAttributeNull(property, "N");
attrs.copy = !property_isAttributeNull(property, "C");
attrs.strong = attrs.copy || !property_isAttributeNull(property, "&");
attrs.weak = !property_isAttributeNull(property, "W");
attrs.is_readonly = !property_isAttributeNull(property, "R");
attrs.is_dynamic = !property_isAttributeNull(property, "D");
return attrs;
}
static objc_property_t class_getPropertyForSelector(Class kls, SEL cmd) {
#define VALID_PROPERTY(property) \
(property != NULL && (property_getGetterSelector(property) == cmd || property_getSetterSelector(property) == cmd))
const char *selName = sel_getName(cmd);
objc_property_t results = class_getProperty(kls, selName);
if (VALID_PROPERTY(results))
return results;
if (strstr(selName, "set") == selName) {
char lowercaseSel[512];
strncpy(lowercaseSel, strstr(selName, "set"), 512);
lowercaseSel[0] = tolower(lowercaseSel[0]);
results = class_getProperty(kls, lowercaseSel);
if (VALID_PROPERTY(results)) return results;
}
// Easy paths exhausted, go the 'hard' way of looping over all of the properties available
results = NULL;
unsigned propertyCount = 0;
objc_property_t *properties = class_copyPropertyList(kls, &propertyCount);
for (unsigned propertyIndex = 0; propertyIndex < propertyCount; propertyIndex++) {
if (VALID_PROPERTY(properties[propertyIndex])) {
results = properties[propertyIndex];
break;
}
}
free(properties);
return results;
#undef VALID_PROPERTY
}
#implementation DictionaryBackedObject
-(id) initWithDictionary:(NSDictionary *) dictionary mutable:(BOOL) isMutable {
return nil;
}
+(Class) dictionaryBackedSubclassOfClass:(Class) kls {
#synchronized (kls) {
NSString *className = [NSStringFromClass(kls) stringByAppendingFormat:#"_dictionaryBacked"];
Class subclass = Nil;
if ((subclass = NSClassFromString(className))) {
return subclass;
}
subclass = objc_allocateClassPair(kls, [className UTF8String], 0);
class_addIvar(subclass, "_backingDictionary", sizeof(NSDictionary *), _Alignof(NSDictionary *), #encode(NSDictionary *));
class_addIvar(subclass, "_backingDictionaryIsMutable", sizeof(NSNumber *), _Alignof(NSNumber *), #encode(NSNumber *));
unsigned propertyCount = 0;
objc_property_t *properties = class_copyPropertyList(kls, &propertyCount);
for (unsigned i = 0; i < propertyCount; i++) {
objc_property_t property = properties[i];
char *type = property_copyAttributeValue(property, "T");
SEL getterSel = property_getGetterSelector(property);
SEL setterSel = property_getSetterSelector(property);
char getterTypeBuffer[512];
snprintf(getterTypeBuffer, 512, "%s#:", type);
char setterTypeBuffer[512];
snprintf(setterTypeBuffer, 512, "v#:%s", type);
NSUInteger typeSize;
NSUInteger typeAlignment;
NSGetSizeAndAlignment(type, &typeSize, &typeAlignment);
BOOL isStret = (typeSize * CHAR_BIT) > (WORD_BIT * 2);
class_addMethod(subclass, getterSel, isStret ? _objc_msgForward_stret : _objc_msgForward , getterTypeBuffer);
class_addMethod(subclass, setterSel, _objc_msgForward, setterTypeBuffer);
free(type);
}
free(properties);
Ivar backingDictionaryIvar = class_getInstanceVariable(subclass, "_backingDictionary");
Ivar backingDictionaryMutableIvar = class_getInstanceVariable(subclass, "_backingDictionaryIsMutable");
class_addMethod(subclass, #selector(forwardingTargetForSelector:), imp_implementationWithBlock(^id (id self) {
return nil;
}), "##:");
class_addMethod(subclass, #selector(forwardInvocation:), imp_implementationWithBlock(^void (id self, NSInvocation *invocation) {
SEL _cmd = [invocation selector];
objc_property_t property = class_getPropertyForSelector([self class], _cmd);
if (property == NULL) {
[self doesNotRecognizeSelector:_cmd];
return;
}
BOOL isGetter = (_cmd == property_getGetterSelector(property));
struct objc_property_attributes_t attributes = property_getPropertyAttributes(property);
NSString *propertyType = (__bridge_transfer NSString *) CFStringCreateWithCStringNoCopy(
NULL, property_copyAttributeValue(property, "T"), kCFStringEncodingUTF8, NULL
);
NSUInteger propertySize;
NSGetSizeAndAlignment([propertyType UTF8String], &propertySize, NULL);
void *dlsymKey = dlsym(RTLD_MAIN_ONLY, property_getName(property));
id dictionaryKey = *(__unsafe_unretained id *) dlsymKey;
NSMutableDictionary *backingDictionary = object_getIvar(self, backingDictionaryIvar);
NSNumber *isMutable = object_getIvar(self, backingDictionaryMutableIvar);
// Performing synchronization on nil is a no-op, see objc_sync.mm:306.
#synchronized (attributes.nonatomic ? nil : self) {
if (isGetter) {
id value = backingDictionary[dictionaryKey];
if (attributes.strong) {
[invocation setReturnValue:&value];
} else if (attributes.weak) {
value = [value nonretainedObjectValue];
[invocation setReturnValue:&value];
} else {
void *buffer = alloca(propertySize);
[value getValue:buffer];
[invocation setReturnValue:buffer];
}
} else {
if ((attributes.is_readonly || ![isMutable boolValue])) {
[self doesNotRecognizeSelector:_cmd];
return;
}
id dictionaryValue = nil;
void *newValue = alloca(propertySize);
[invocation getArgument:newValue atIndex:2];
if (attributes.strong) {
dictionaryValue = (__bridge id) newValue;
if (attributes.copy) {
dictionaryValue = [dictionaryValue copy];
}
} else if (attributes.weak) {
dictionaryValue = [NSValue valueWithNonretainedObject:(__bridge id) newValue];
} else {
dictionaryValue = [NSValue valueWithBytes:newValue objCType:[propertyType UTF8String]];
}
if (dictionaryValue == nil) {
[backingDictionary removeObjectForKey:dictionaryKey];
} else {
[backingDictionary setObject:dictionaryValue forKey:dictionaryKey];
}
}
}
}), "v#:#");
class_addMethod(subclass, #selector(initWithDictionary:mutable:), imp_implementationWithBlock(^id (id self, NSDictionary *dictionary, BOOL mutable) {
object_setIvar(self, backingDictionaryIvar, dictionary);
object_setIvar(self, backingDictionaryMutableIvar, #(mutable));
return self;
}), "##:#c");
objc_registerClassPair(subclass);
return subclass;
}
}
+(id) dictionaryBackedObjectOfType:(Class)kls backingDictionary:(NSDictionary *)dictionary mutable:(BOOL)isMutable {
Class subclass = [self dictionaryBackedSubclassOfClass:kls];
return [[subclass alloc] initWithDictionary:dictionary mutable:isMutable];
}
#end
Rob Napier's example is a good option; the compiler's going to generate accessors for you unless you tell it not to, and the way you tell it that is with the #dynamic directive.
Another option would be automated code generation: write a script to emit ObjC code for your setters.
The third that I can think of is overwriting the accessors during runtime. In your class's +initialize, you can get the list of its properties from the runtime library and use class_replaceMethod() to insert your own accessors that use your dictionary instead of the ivars. This will require some string mangling to get the accessor names and keys from each other.
Here's a gist with a demo of that last option: https://gist.github.com/woolsweater/4fb874b15449ee7fd7e8
I'm trying to filter an array according to one of it's string fields.
Both nameLower and filterLower has NSString value inside, and yet i keep getting:
__NSCFString containsString:]: unrecognized selector sent to instance 0x7f876b79e160
-(void) filterFriendsArray:(NSString*)filter {
[_filteredFriendsArray removeAllObjects];
for (FacebookUser* user in _friendsArray)
{
NSString* nameLower = [user.user.name lowercaseString];
NSString* filterLower = [filter lowercaseString];
if ([nameLower containsString:filterLower])
[_filteredFriendsArray addObject:user];
}
_displayedFriendsArray = _filteredFriendsArray;
}
If you want your code to work on iOS 7 as well as iOS 8 you should use one of the rangeOfString calls instead. Basically if the range returned has a length of zero, the substring is not there.
/* These methods return length==0 if the target string is not found. So, to check for containment: ([str rangeOfString:#"target"].length > 0). Note that the length of the range returned by these methods might be different than the length of the target string, due composed characters and such.
*/
- (NSRange)rangeOfString:(NSString *)aString;
- (NSRange)rangeOfString:(NSString *)aString options:(NSStringCompareOptions)mask;
- (NSRange)rangeOfString:(NSString *)aString options:(NSStringCompareOptions)mask range:(NSRange)searchRange;
- (NSRange)rangeOfString:(NSString *)aString options:(NSStringCompareOptions)mask range:(NSRange)searchRange locale:(NSLocale *)locale NS_AVAILABLE(10_5, 2_0);
Obviously it's trivial to implement containsString yourself in a category using rangeOfString:
#implementation NSString (Contains)
- (BOOL)myContainsString:(NSString*)other {
NSRange range = [self rangeOfString:other];
return range.length != 0;
}
#end
compare rangeOfString with NSNotFound
NSRange range = [self rangeOfString:other];
if(range.location != NSNotFound){
//do something
}
Use following:
if (![[NSString class] respondsToSelector:#selector(containsString)])
{
//ios 7
NSRange range = [mystring rangeOfString:other];
if(range.location != NSNotFound){
//do something
}
}
else //for iOS 8
{
if ([mystring containsString: other])
{
//do something
}
}
For those who encountered this in XLForm, make sure when you install XLForm using pods
platform :ios, '7'
pod 'XLForm'
It is already fixed in 3.1
from
if ([cellClassString contains:#"/"]) {
}
to
if ([cellClassString rangeOfString:#"/"].location != NSNotFound) {
}
I encapsulate my solution in YJKit, and you can call -[NSString containsString:] even for old version which below iOS 8.
bool _yj_streq(const char *str1, const char *str2, size_t length) {
for (int i = 0; i < length; i++) {
if (*str1++ != *str2++) {
return false;
}
}
return true;
}
- (BOOL)yj_containsString:(NSString *)string {
NSAssert(string != nil, #"*** -[%# containsString:] can not use nil argument.", [self class]);
size_t len1 = (size_t)self.length;
size_t len2 = (size_t)string.length;
if (len1 == 0 || len2 == 0 || len1 < len2) {
return NO;
}
const char *str1 = self.UTF8String;
const char *str2 = string.UTF8String;
for (size_t i = 0; i <= len1 - len2; i++) {
const char *substr1 = str1 + i;
if (_yj_streq(substr1, str2, len2)) {
return YES;
} else {
continue;
}
}
return NO;
}
Here is my source code:
https://github.com/huang-kun/YJKit/blob/master/YJKit/Base/Foundation/Categories/Generics/NSString%2BYJCompatible.m
Swift version of the answer given by w0mbat:
extension NSString {
func compatibleContainsString(string: NSString) -> Bool{
let range = self.rangeOfString(string as String)
return range.length != 0
}
}
My array is returning an incorrect number for an index of an object that I am looking for. My array.count tells me there are 248 objects in the array, but the index that is returned is 2147483647. I saw this post that is similar to mine issue. I used the equality testing that was proposed in this post, but fell short of figuring out why this was happening and how to fix it.
Here is my code: (Edited with solution)
-(void)getChamberPrimaryCategories
{
NSString *docsDir;
NSArray *dirPaths;
chamberCategoryArray = [[NSMutableArray alloc] init];
// Get the documents directory
dirPaths = NSSearchPathForDirectoriesInDomains(
NSDocumentDirectory, NSUserDomainMask, YES);
docsDir = dirPaths[0];
// Build the path to the database file
_databasePath = [[NSString alloc]
initWithString: [docsDir stringByAppendingPathComponent:
#"the_app.db"]];
NSLog(#"%#",_databasePath);
NSFileManager *filemgr = [NSFileManager defaultManager];
if ([filemgr fileExistsAtPath: _databasePath ] == NO)
{
const char *dbpath = [_databasePath UTF8String];
if (sqlite3_open(dbpath, &_the_app_database) == SQLITE_OK)
{
//do nothing
} else {
NSLog(#"Failed to open database");
}
}
const char *dbpath = [_databasePath UTF8String];
sqlite3_stmt *statement;
if (sqlite3_open(dbpath, &_the_app_database) == SQLITE_OK)
{
NSString *querySQL;
querySQL = #"SELECT DISTINCT CHAMBER_PRIMARY_CATEGORY FROM PLACES WHERE CHAMBER_PRIMARY_CATEGORY IS NOT '' AND CHAMBER_PRIMARY_CATEGORY IS NOT NULL ORDER BY CHAMBER_PRIMARY_CATEGORY ASC";
const char *query_stmt = [querySQL UTF8String];
if (sqlite3_prepare_v2(_the_app_database,
query_stmt, -1, &statement, NULL) == SQLITE_OK)
{
NSLog(#"Query the database now.");
// if (sqlite3_step(statement) != SQLITE_ROW) {
// NSLog(#"Not okay");
// }
while (sqlite3_step(statement) == SQLITE_ROW) {
NSLog(#"Getting Information:");
placesChamber *chamberCategoryObject = [[placesChamber alloc]init];
NSString *placesPrimaryChamberCategory = [[NSString alloc]
initWithUTF8String:
(const char *) sqlite3_column_text(
statement, 0)];
chamberCategoryObject.primary_category = placesPrimaryChamberCategory;
NSLog(#"%#",placesPrimaryChamberCategory);
[chamberCategoryArray addObject:chamberCategoryObject];
}//end while
}//end if
else
{
NSLog(#"There is nothing in this database!");
NSLog(#"%s",sqlite3_errmsg(_the_kearney_app_database));
}
sqlite3_finalize(statement);
sqlite3_close(_the_kearney_app_database);
[chamberCategoryTableView reloadData];
NSLog(#"content offset: %f",chamberCategoryTableView.contentOffset.y);
if (chamberCategoryTableView.contentOffset.y == 0)
{
placesChamber *searchChamber = [[placesChamber alloc] init];
searchChamber.primary_category = #"Women's Clothing";
NSUInteger index = [chamberCategoryArray indexOfObject:searchChamber];
if (index == NSNotFound) {
// no such chamber category
NSLog(#"no such thing found");
} else {
// Found it at index
NSLog(#"found it!");
}
}//end if
}//end if
}
Here is my placesChamber.m: (Needed for solution)
#import "placesChamber.h"
#implementation placesChamber
#synthesize ID;
#synthesize name;
#synthesize type;
#synthesize category;
#synthesize latitude;
#synthesize longitude;
#synthesize primary_category;
#synthesize secondary_category;
- (BOOL)isEqual:(id)object
{
if ( self == object ) {
return YES;
}
if ( ![object isKindOfClass:[placesChamber class]] ) {
return NO;
}
if ( ![primary_category isEqualToString:[object primary_category]] ) {
return NO;
}
return YES;
}
#end
As I stated in the comments, you have populated your chamberCategoryArray with instances of placesChamber objects.
But you then attempt to find the index of one of these objects not by passing in a placesChamber object but by passing in an NSString literal. That simply won't work.
You need to do something like this:
placesChamber *searchChamber = [[placesChamber alloc] init];
searchChamber.primary_category = #"Women's Clothing";
NSUInteger index = [chamberCategoryArray indexOfObject:searchChamber];
if (index == NSNotFound) {
// no such chamber category
} else {
// Found it at index
}
This code assumes you have implemented the isEqual: method on your placesChamber class that compares the primary_category property.
Side notes: It is standard convention that classnames begin with uppercase letters while method names and variables begin with lowercase. All should use camel case.
Given this your class should be PlacesChamber and the property should be primaryCategory.
I found that you have used indexOfObject: in the following 3 lines of code:
//1.
NSIndexPath *scrollIndexPath = [NSIndexPath indexPathForItem:[chamberCategoryArray indexOfObject:[defaults objectForKey:#"chamber filter category"]] inSection:0];
//2.
NSLog(#"index number: %lu",(unsigned long)[chamberCategoryArray indexOfObject:#"Women's Clothing"]);
//3.
if(NSNotFound == [chamberCategoryArray indexOfObject:[NSNumber numberWithInteger:1]]) {
NSLog(#"not found");
}
And you are populating your chamberCategoryArray in the following line which is populating the array with an object of type placesChamber:
[chamberCategoryArray addObject:chamberCategoryObject];
Now if you pass an indexOfObject: message to an NSArray (or its subclass), it iterates through the array sending each object of the array an isEqual: message for equality and if one found, it returns the index, otherwise returns NSNotFound (as you are getting). And this equality comparison is based on the calculated hash code for each contained object, that is, if one object in the array has the same hash code (calculated internally) as the object it is being compared to, those are treated as equal.
In your example code:
In 1. you are checking the hash codes of the objects of the array
with the hash code of [defaults objectForKey:#"chamber filter
category"] and I'm not sure what defaults made of.
In 2. you are checking the elements against a NSString. But the
objects of the array are of type placesChamber and hash codes for
two different types never match and you would always get NSNotFound
as a result.
In 3. you are doing the same error, this time with a NSNumber
instead of a NSString. So the above logic holds here also.
So it's very obvious you would get NSNotFound (2147483647) as a occasional result.
I believe you are trying to return the index of an object from the array(which is actually a placesChamber by type) that has a property matching your test criteria. For that purpose you might use indexOfObjectPassingTest: method that takes a block to write your comparison logic. Inside the block, you can write your own customized logic of matching an array item with your search criteria and the object index would be returned accordingly.
Update
This what you are actually looking for at the first place:
NSString* criteria = #"Women's Clothing";
int index = [chamberCategoryArray indexOfObjectPassingTest:^BOOL(id obj, NSUInteger idx, BOOL *stop) {
placesChamber* item = obj;
*stop = [item.primary_category isEqualToString:criteria];
return *stop;
}];
Here I have demonstrated how to obtain the index based on primary_category property of placesChamber objects. The same method holds for other properties as well.
Hello I'm trying to to something rather simple I think.
I have made an cocoa application that sends data using APNS, getting the tokens from my database, everything is set up and running perfect.
Now I want to check the APNS feedback server and remove any tokens received from my database.
I have found dozens of examples in php, javascript and so forth, but nothing in Objective C. I have read the programming guide from apple but can't figure out how to do it.
I am establishing a connection to APNS feedback but I don't know how to read the data.
I'm new to cocoa so please explain in detail :)
This is how I connect to the feedback server, it's the same way I connect when sending, just using another host.
- (void)connectToFeedBackServer
{
if(self.certificate == nil)
{
return;
}
NSString *feedBackHost = #"feedback.push.apple.com";
const char *cHost = [feedBackHost UTF8String];
NSLog(#"The size of cHost is: %lu", strlen(cHost));
NSLog(#"Host is: %s", cHost);
// Define result variable.
OSStatus result;
// Establish connection to server.
PeerSpec peer;
result = MakeServerConnection(cHost, 2196, &socket, &peer);
//NSLog(#"MakeServerConnection(): %d", result);
// Create new SSL context.
result = SSLNewContext(false, &context); //NSLog(#"SSLNewContext(): %d", result);
// Set callback functions for SSL context.
result = SSLSetIOFuncs(context, SocketRead, SocketWrite);
// NSLog(#"SSLSetIOFuncs(): %d", result);
// Set SSL context connection.
result = SSLSetConnection(context, socket);
// NSLog(#"SSLSetConnection(): %d", result);
// Set server domain name.
//result = SSLSetPeerDomainName(context, cHost, sizeof(cHost));
NSLog(#"SSLSetPeerDomainName(): %d", result);
result = SSLSetPeerDomainName(context, cHost, strlen(cHost));
result = SecIdentityCopyCertificate(_theIdentity, &(certificate));
// Set client certificate.
CFArrayRef certificates = CFArrayCreate(NULL, (const void **)&_theIdentity, 1, NULL);
result = SSLSetCertificate(context, certificates);// NSLog(#"SSLSetCertificate(): %d", result);
CFRelease(certificates);
// Perform SSL handshake.
do
{
result = SSLHandshake(context); NSLog(#"SSLHandshake(): %d", result);
} while(result == errSSLWouldBlock);
}
And how I try to read the data and save the received the tokens in an array
- (NSMutableArray *)CheckFeedBackServer
{
char feedback[38];
size_t feedBackSize = sizeof(feedback);
size_t processed = 0;
NSMutableData *feedbackData = [[NSMutableData alloc]init];
NSString *token = [[NSString alloc]init];
NSMutableArray *tokenArray = [[NSMutableArray alloc]init];
[self connectToFeedBackServer];
while ([self getSSLContext])
{
int bytesLength = SSLRead([self getSSLContext], &feedback, feedBackSize, &processed);
[feedbackData appendBytes:feedback length:bytesLength];
while ([feedbackData length] > 38)
{
NSData *deviceToken = [NSData dataWithBytes:[feedbackData bytes] + 6 length:32];
token = [self deviceTokenToString:deviceToken];
[tokenArray addObject:token];
[feedbackData replaceBytesInRange: NSMakeRange(0, 38) withBytes: "" length: 0];
}
}
return tokenArray;
}
- (NSString *)deviceTokenToString: (NSData *)deviceToken;
{
NSString *tmpToken = [NSString stringWithFormat:#"%#", deviceToken];
NSUInteger loc_begin = [tmpToken rangeOfString: #"<"].location+1;
NSUInteger loc_end = [tmpToken rangeOfString: #">"].location-1;
return [tmpToken substringWithRange: NSMakeRange(loc_begin, loc_end)];
}
Just if anyone need to do something similar I solved my problem like this.
I use Apples ioSock class, and I have set the certificate in my code by calling the keychain
First I connect to the feedback server with this code
- (void)connectToFeedBackServer
{
if(self.certificate == nil)
{
return;
}
// Get the global variable feedbackHost and make it to a char
const char *cHost = [feedbackHost UTF8String];
NSLog(#"The size of cHost is: %lu", strlen(cHost));
NSLog(#"Host is: %s", cHost);
// Define result variable.
OSStatus result;
// Establish connection to server.
PeerSpec peer;
result = MakeServerConnection(cHost, 2196, &socket, &peer);
// Create new SSL context.
result = SSLNewContext(false, &context);
// Set callback functions for SSL context.
result = SSLSetIOFuncs(context, SocketRead, SocketWrite);
// Set SSL context connection.
result = SSLSetConnection(context, socket);
// Set server domain name.
result = SSLSetPeerDomainName(context, cHost, strlen(cHost));
result = SecIdentityCopyCertificate(_theIdentity, &(certificate));
// Set client certificate.
CFArrayRef certificates = CFArrayCreate(NULL, (const void **)&_theIdentity, 1, NULL);
result = SSLSetCertificate(context, certificates);
CFRelease(certificates);
do
{
result = SSLHandshake(context); NSLog(#"SSLHandshake(): %d", result);
} while(result == errSSLWouldBlock);
}
And then I read the feedback data and add the tokens to an array, like this
- (NSMutableArray *)CheckFeedBackServer
{
OSStatus result;
NSMutableArray *feedbackTokens = [[NSMutableArray alloc]init];
// Retrieve message from SSL.
size_t processed = 0;
char buffer[38];
do
{
// Fetch the next item
result = SSLRead(context, buffer, 38, &processed);
if (result) break;
char *b = buffer;
// Recover Device ID
NSMutableString *deviceID = [NSMutableString string];
b += 6;
for (int i = 0; i < 32; i++)
{
[deviceID appendFormat:#"%02x", (unsigned char)b[i]];
}
[feedbackTokens addObject:deviceID];
} while (processed > 0);
return feedbackTokens;
}