Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access to custom Obj-C category from Swift

I'm trying to access this custom obj-C category in Swift file:

#import "NSString+UUID.h"

@implementation NSString (UUIDCategory)

+ (NSString*)stringWithUUID 
{
    CFUUIDRef uuid = CFUUIDCreate(kCFAllocatorDefault);
    NSString *string = (NSString*)CFMakeCollectable(CFUUIDCreateString(kCFAllocatorDefault, uuid));
    CFRelease(uuid);
    return [string autorelease];    
}

@end

So, I added :

#import "NSString+UUID.h" 

in my bridging header, but can't find a solution to use it.

var string = NSString.stringWithUUID()

EDIT: the code seems to work now (the problem was I didn't include #import Foundation/Foundation.h in my bridging header before importing my categories.

like image 449
alex.bour Avatar asked Jan 26 '26 09:01

alex.bour


1 Answers

This is a factory method and Swift will create an initializer for you automatically!

You should be able to use it like this:

 let string = NSString(UUID:uuid)

Off topic, but you should really adopt ARC :]

like image 184
Jack Avatar answered Jan 28 '26 05:01

Jack



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!