Skip to content

Commit

Permalink
Small fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
shoumikhin committed Jan 21, 2013
1 parent 11d0d3c commit 2a2f18f
Show file tree
Hide file tree
Showing 9 changed files with 46 additions and 22 deletions.
4 changes: 2 additions & 2 deletions NSFileManagerX.m
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ + (NSURL *)URLForDirectory:(NSSearchPathDirectory)directory

+ (NSString *)pathForDirectory:(NSSearchPathDirectory)directory
{
return NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
return NSSearchPathForDirectoriesInDomains(directory, NSUserDomainMask, YES)[0];
}

+ (NSURL *)documentsURL
Expand Down Expand Up @@ -60,7 +60,7 @@ + (BOOL)addSkipBackupAttributeToFile:(NSString *)path
if (-1 != getxattr(filePath, attrName, NULL, sizeof(u_int8_t), 0, 0))
removexattr(filePath, attrName, 0);

return [[NSURL fileURLWithPath:path] setResourceValue:@(YES) forKey:NSURLIsExcludedFromBackupKey error:nil];
return [[NSURL.alloc initFileURLWithPath:path] setResourceValue:@(YES) forKey:NSURLIsExcludedFromBackupKey error:nil];
}
}

Expand Down
11 changes: 9 additions & 2 deletions NSManagedObjectContextX.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,14 @@
@param limit A fetch limit of objects to return. Zero means unlimited.
@return An array of fetched objects.
*/
- (NSArray *)objectsOfEntity:(NSString *)name withPredicate:(NSPredicate *)predicate sortDecriptors:(NSArray *)sortDescriptors andFetchLimit:(NSUInteger)limit;
- (NSArray *)objectsOfEntity:(NSString *)name withPredicate:(NSPredicate *)predicate sortDescriptors:(NSArray *)sortDescriptors andFetchLimit:(NSUInteger)limit;

/**
Delete several objects.
@param objects Objects to delete.
*/
- (void)deleteObjects:(NSArray *)objects;

/**
Delete several objects.
Expand All @@ -36,6 +43,6 @@
@param sortDescriptors A list of sorting rules.
@param limit A fetch limit of objects to delete. Zero means unlimited.
*/
- (void)deleteObjectsOfEntity:(NSString *)name withPredicate:(NSPredicate *)predicate sortDecriptors:(NSArray *)sortDescriptors andFetchLimit:(NSUInteger)limit;
- (void)deleteObjectsOfEntity:(NSString *)name withPredicate:(NSPredicate *)predicate sortDescriptors:(NSArray *)sortDescriptors andFetchLimit:(NSUInteger)limit;

@end
21 changes: 12 additions & 9 deletions NSManagedObjectContextX.m
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ @implementation NSManagedObjectContext (X)

- (NSManagedObject *)newObjectOfEntity:(NSString *)name
{
return [NSManagedObject.alloc initWithEntity:[NSEntityDescription entityForName:name inManagedObjectContext:self] insertIntoManagedObjectContext:self];
return [NSManagedObject.alloc initWithEntity:self.persistentStoreCoordinator.managedObjectModel.entitiesByName[name] insertIntoManagedObjectContext:self];
}

- (NSArray *)objectsOfEntity:(NSString *)name withPredicate:(NSPredicate *)predicate sortDecriptors:(NSArray *)sortDescriptors andFetchLimit:(NSUInteger)limit
- (NSArray *)objectsOfEntity:(NSString *)name withPredicate:(NSPredicate *)predicate sortDescriptors:(NSArray *)sortDescriptors andFetchLimit:(NSUInteger)limit
{
NSFetchRequest *request = NSFetchRequest.new;
request.entity = [NSEntityDescription entityForName:name inManagedObjectContext:self];

request.entity = self.persistentStoreCoordinator.managedObjectModel.entitiesByName[name];
request.predicate = predicate;
request.fetchLimit = limit;

Expand All @@ -21,17 +21,20 @@ - (NSArray *)objectsOfEntity:(NSString *)name withPredicate:(NSPredicate *)predi
return [self executeFetchRequest:request error:nil];
}

- (void)deleteObjectsOfEntity:(NSString *)name withPredicate:(NSPredicate *)predicate sortDecriptors:(NSArray *)sortDescriptors andFetchLimit:(NSUInteger)limit
- (void)deleteObjects:(NSArray *)objects
{
for (NSManagedObject *object in objects)
@try
{
NSArray *toRemove = [self objectsOfEntity:name withPredicate:predicate sortDecriptors:sortDescriptors andFetchLimit:limit];

for (NSManagedObject *object in toRemove)
[self deleteObject:object];
[self deleteObject:object];
}
@catch (NSException *exception)
{}
}

- (void)deleteObjectsOfEntity:(NSString *)name withPredicate:(NSPredicate *)predicate sortDescriptors:(NSArray *)sortDescriptors andFetchLimit:(NSUInteger)limit
{
[self deleteObjects:[self objectsOfEntity:name withPredicate:predicate sortDescriptors:sortDescriptors andFetchLimit:limit]];
}

@end
13 changes: 12 additions & 1 deletion NSURLX.m
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,18 @@ @implementation NSURL (X)

- (NSURL *)hostURL
{
return [NSURL URLWithString:[NSString stringWithFormat:@"%@://%@", self.scheme, self.host]];
NSMutableString *ret = NSMutableString.new;

if (self.scheme.length)
[ret appendFormat:@"%@://", self.scheme];

if (self.host.length)
[ret appendString:self.host];

if (self.port)
[ret appendFormat:@":%@", self.port];

return [NSURL.alloc initWithString:ret];
}

@end
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,13 @@ to your precompiled header, and you'll boost Cocoa with the following stuff (in
- (NSManagedObject *)newObjectOfEntity:(NSString *)name;

//fetch objects with options, zero limit means unlimited
- (NSArray *)objectsOfEntity:(NSString *)name withPredicate:(NSPredicate *)predicate sortDecriptors:(NSArray *)sortDescriptors andFetchLimit:(NSUInteger)limit;
- (NSArray *)objectsOfEntity:(NSString *)name withPredicate:(NSPredicate *)predicate sortDescriptors:(NSArray *)sortDescriptors andFetchLimit:(NSUInteger)limit;

//delete multiple objects
- (void)deleteObjects:(NSArray *)objects;

//delete objects with options
- (void)deleteObjectsOfEntity:(NSString *)name withPredicate:(NSPredicate *)predicate andSortDecriptors:(NSArray *)sortDescriptors;
- (void)deleteObjectsOfEntity:(NSString *)name withPredicate:(NSPredicate *)predicate andSortDescriptors:(NSArray *)sortDescriptors;

#### NSMutableArray

Expand Down
2 changes: 1 addition & 1 deletion UIApplicationX.m
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ + (NSArray *)backtrace
void *stack[0x80];
int count = backtrace(stack, sizeof(stack)/sizeof(stack[0]));
char **strings = backtrace_symbols(stack, count);
NSMutableArray *ret = [NSMutableArray arrayWithCapacity:count];
NSMutableArray *ret = [NSMutableArray.alloc initWithCapacity:count];

for (int i = 0; i < count; ++i)
[ret addObject:@(strings[i])];
Expand Down
4 changes: 2 additions & 2 deletions UIDeviceX.m
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ + (BOOL)isPad8Inch

sysctlbyname("hw.machine", machine, &size, NULL, 0);

NSString *platform = [NSString stringWithUTF8String:machine];
NSString *platform = [NSString.alloc initWithUTF8String:machine];

free(machine);

Expand Down Expand Up @@ -77,7 +77,7 @@ + (NSString *)MacAddressOfInterface:(NSString *)interface
sdl = (struct sockaddr_dl *)(ifm + 1);
mac = (unsigned char *)LLADDR(sdl);

NSString *ret = [NSString stringWithFormat:@"%02X:%02X:%02X:%02X:%02X:%02X", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]];
NSString *ret = [NSString.alloc initWithFormat:@"%02X:%02X:%02X:%02X:%02X:%02X", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]];

free(buf);

Expand Down
4 changes: 2 additions & 2 deletions UIImageX.m
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ @implementation UIImage (X)

- (id)initWithContentsOfURL:(NSURL *)URL
{
return [self initWithData:[NSData dataWithContentsOfURL:URL]];
return [self initWithData:[NSData.alloc initWithContentsOfURL:URL]];
}

+ (UIImage *)imageWithContentsOfURL:(NSURL *)URL
{
return [UIImage imageWithData:[NSData dataWithContentsOfURL:URL]];
return [UIImage imageWithData:[NSData.alloc initWithContentsOfURL:URL]];
}

@end
2 changes: 1 addition & 1 deletion UIWebViewX.m
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ @implementation UIWebView (X)

- (void)loadHTMLString:(NSString *)string baseURL:(NSURL *)baseURL font:(UIFont *)font
{
[self loadHTMLString:[NSString stringWithFormat:@"<html> \n<style type=\"text/css\"> \nbody {font-family: \"%@\"; font-size: %f;}\n</style> \n</head> \n<body>%@</body> \n</html>", font.fontName, font.pointSize, string] baseURL:baseURL];
[self loadHTMLString:[NSString.alloc initWithFormat:@"<html> \n<style type=\"text/css\"> \nbody {font-family: \"%@\"; font-size: %f;}\n</style> \n</head> \n<body>%@</body> \n</html>", font.fontName, font.pointSize, string] baseURL:baseURL];
}

- (void)clearCookies
Expand Down

0 comments on commit 2a2f18f

Please sign in to comment.