working on it ...

Filters

Explore Public Snippets

Sort by

Found 1,685 snippets matching: objective

    public by fromano modified Oct 10, 2012  1679  0  6  0

    Objective C Singleton pattern

    Implement singleton pattern in Objective C, the ARC-way.
    + (MyClass*)sharedManager {
    	static dispatch_once_t onceToken;
    	__strong static MyClass *instance = nil;
    	dispatch_once(&onceToken, ^{
    		instance = [[self alloc] init];
    	});
    	return instance;
    }

    public by marksimon232 modified Jun 4, 2014  4330  0  6  2

    Xcode 6 - Language options to create the project using swift ios 8 application development? Default Objective C?

    I heard lot of questions asking From xcode 6, when using the menu File -> new -> project -> application
    , it always creates the project using objective C.  what is the option to create the project using swift?
    
    This is the answer of your question:
    
    There is an option to change the language 
    to Swift. It’s on the next page after you select your project type. 
    
    See the snap here:
    http://tinypic.com/r/2pzbtrb/8
    
    Read more:
    http://www.agileinfoways.com/technical-expertise/mobile-applications-development/iphone/
    

    public by marksimon232 modified Jun 23, 2014  5832  8  6  3

    Base64 Decoding in iOS 7+

    Do you have Encoded text(NSString) using NSData Class new API which is Added in iOS7?
    Objective-C
    
    NSString *plainString = @"foo";
    Encoding
    
    NSData *plainData = [plainString dataUsingEncoding:NSUTF8StringEncoding];
    NSString *base64String = [plainData base64EncodedStringWithOptions:0];
    NSLog(@"%@", base64String); // Zm9v
    Decoding
    
    NSData *decodedData = [[NSData alloc] initWithBase64EncodedString:base64String options:0];
    NSString *decodedString = [[NSString alloc] initWithData:decodedData encoding:NSUTF8StringEncoding];
    NSLog(@"%@", decodedString); // foo 
    Swift
    
    let plainString = "foo";
    Encoding
    
    let plainData = (plainString as NSString).dataUsingEncoding(NSUTF8StringEncoding)
    let base64String = plainData.base64EncodedStringWithOptions(NSDataBase64EncodingOptions.fromRaw(0)!)
    println(base64String) // Zm9v
    Decoding
    
    let decodedData = NSData(base64EncodedString: base64String, options: NSDataBase64DecodingOptions.fromRaw(0)!)
    let decodedString = NSString(data: decodedData, encoding: NSUTF8StringEncoding)    
    println(decodedString); // foo 

    public by marksimon232 modified Jul 2, 2014  4591  7  7  8

    Efficient ModExp for Cryptographic purpose in C

    How to implement an efficient routine to calculate the Modular exponentiation as stated in http://en.wikipedia.org/wiki/Modular_exponentiation.
    Using modular multiplication rules:
    i.e. A^2 mod C = (A * A) mod C = ((A mod C) * (A mod C)) mod C
    We can use this to calculate 7^256 mod 13 quickly
    7^1 mod 13 = 7
    7^2 mod 13 = (7^1 *7^1) mod 13 = (7^1 mod 13 * 7^1 mod 13) mod 13
    We can substitute our previous result for 7^1 mod 13 into this equation.
    7^2 mod 13 = (7 *7) mod 13 = 49 mod 13 = 10
    7^2 mod 13 = 10
    7^4 mod 13 = (7^2 *7^2) mod 13 = (7^2 mod 13 * 7^2 mod 13) mod 13
    We can substitute our previous result for 7^2 mod 13 into this equation.
    7^4 mod 13 = (10 * 10) mod 13 = 100 mod 13 = 9
    7^4 mod 13 = 9
    7^8 mod 13 = (7^4 * 7^4) mod 13 = (7^4 mod 13 * 7^4 mod 13) mod 13
    We can substitute our previous result for 7^4 mod 13 into this equation.
    7^8 mod 13 = (9 * 9) mod 13 = 81 mod 13 = 3
    7^8 mod 13 = 3
    We continue in this manner, substituting previous results into our equations.
    ...after 5 iterations we hit:
    7^256 mod 13 = (7^128 * 7^128) mod 13 = (7^128 mod 13 * 7^128 mod 13) mod 13
    7^256 mod 13 = (3 * 3) mod 13 = 9 mod 13 = 9
    7^256 mod 13 = 9
    This has given us a method to calculate A^B mod C quickly provided that B is a power of 2.
    However, we also need a method for fast modular exponentiation when B is not a power of 
    
    Let say.. How can we calculate A^B mod C quickly for any B ?
    => Step 1: Divide B into powers of 2 by writing it in binary
    Start at the rightmost digit, let k=0 and for each digit:
    
    If the digit is 1, we need a part for 2^k, otherwise we do not
    Add 1 to k, and move left to the next digit
    
    => Step 2: Calculate mod C of the powers of two ≤ B
    5^1 mod 19 = 5
    5^2 mod 19 = (5^1 * 5^1) mod 19 = (5^1 mod 19 * 5^1 mod 19) mod 19
    5^2 mod 19 = (5 * 5) mod 19 = 25 mod 19
    5^2 mod 19 = 6
    5^4 mod 19 = (5^2 * 5^2) mod 19 = (5^2 mod 19 * 5^2 mod 19) mod 19
    5^4 mod 19 = (6 * 6) mod 19 = 36 mod 19
    5^4 mod 19 = 17
    5^8 mod 19 = (5^4 * 5^4) mod 19 = (5^4 mod 19 * 5^4 mod 19) mod 19
    5^8 mod 19 = (17 * 17) mod 19 = 289 mod 19
    5^8 mod 19 = 4
    5^16 mod 19 = (5^8 * 5^8) mod 19 = (5^8 mod 19 * 5^8 mod 19) mod 19
    5^16 mod 19 = (4 * 4) mod 19 = 16 mod 19
    5^16 mod 19 = 16
    5^32 mod 19 = (5^16 * 5^16) mod 19 = (5^16 mod 19 * 5^16 mod 19) mod 19
    5^32 mod 19 = (16 * 16) mod 19 = 256 mod 19
    5^32 mod 19 = 9
    5^64 mod 19 = (5^32 * 5^32) mod 19 = (5^32 mod 19 * 5^32 mod 19) mod 19
    5^64 mod 19 = (9 * 9) mod 19 = 81 mod 19
    5^64 mod 19 = 5
    
    => Step 3: Use modular multiplication properties to combine the calculated mod C values
    5^117 mod 19 = ( 5^1 * 5^4 * 5^16 * 5^32 * 5^64) mod 19
    5^117 mod 19 = ( 5^1 mod 19 * 5^4 mod 19 * 5^16 mod 19 * 5^32 mod 19 * 5^64 mod 19) mod 19
    5^117 mod 19 = ( 5 * 17 * 16 * 9 * 5 ) mod 19
    5^117 mod 19 = 61200 mod 19 = 1
    5^117 mod 19 = 1
    
    Notes:
    More optimization techniques exist, but are outside the scope of this snippet. It should be noted that when we perform modular exponentiation in cryptography, it is not unusual to use exponents for B > 1000 bits.
    

    public by marksimon232 modified Jul 3, 2014  5387  2  9  2

    Swift Collections: Writing a Switch statement

    Objective-C to Swift
    In Swift, switch statements are a lot more flexible and can be used to test a variety of comparison operations. In Objective-C, switch statements were limited to integer cases.
    
    Objective-C:
    int numberOfPeople = 1;
     
    switch (numberOfPeople)
    {
        case 1:
            // code for this case
            break;
        case 2:
            // code for this case
            break;
        default:
            // code for this case
            break;
    }
    
    ========THIS CODE WILL BE WRITTEN AS:==============
    
    Swift:
    Notice that you don’t need the break statements anymore!
    
    var numberOfPeople = 1
     
    switch numberOfPeople {
        case 1:
            // code for this case
        case 2:
            // code for this case
        default:
            // code for this case
    }
     
    var carMake = "Toyota"
     
    switch carMake {
        case "Toyota":
            // code for this case
        case "Honda":
            // code for this case
        case "Nissan", "Subaru":
            // code for this case
        default:
            // code for this case
    }
    

    public by marksimon232 modified Jul 11, 2014  4078  0  6  4

    Swift Collections: Declaring a delegate property

    Delegation works hand in hand with protocols because it allows a class to specify a delegate property which conforms to some protocol.
    Objective-C:
    In Objective-C, declaring a delegate property involved using the “id” 
    keyword as shown below.
    
    @interface FirstClass : NSObject
    @property (nonatomic, weak) id<SampleProtocol> delegate;
    @end
    
    -----------------------
    
    Swift:
    In Swift, declaring a delegate property is just like declaring any other 
    property and you specify the protocol name as the type of the property.
    
    You may notice the question mark syntax which indicates that it’s a property 
    with an optional value (there may or may not be an object assigned to it).
    
    class FirstClass
    {
        var delegate:SampleProtocol?
    }

    public by marksimon232 modified Jul 3, 2014  4233  1  6  1

    Swift Collections: Declaring an array

    Objective-C to Swift
    Arrays in Objective-C used the NSArray and NSMutableArray classes and each array could contain a mixture of objects of different class types. In Swift, there’s one Array class and you have to specify the type of objects that the array will contain.
    
    Objective-C:
    NSMutableArray *myArray = [[NSMutableArray alloc] init];
    NSMutableArray *myArray2 = [@[@"item 1", @"item 2"] mutableCopy];
    
    ========THIS CODE WILL BE WRITTEN AS:==============
    
    Swift:
    Notice that in the second and third examples, we don’t specify that the variable is of type String Array uses because it can be inferred from the array that we’re assigning it. From the assigned array items, Swift can infer that the variable is an Array and it contains String objects.
    We don’t need to specify the variable type because it can be inferred from what we’re assigning to it.
    
    var myArray:String[] = String[]()
    var myArray2 = ["item 1", "item 2"]

    public by fahad modified Feb 11, 2015  4676  0  6  0

    Example of using boundingRectWithSize:options:attributes:context:

    ///////////// This will
    //        CGSize titleSize = [self.titleLabel.text sizeWithFont:self.titleLabel.font
    //                                            constrainedToSize:CGSizeMake(labelMaxWidth,self.titleLabel.font.lineHeight)
    //                                                lineBreakMode:self.titleLabel.lineBreakMode];
            
    ////////////// Changed To
            NSMutableParagraphStyle * paragraphStyle1 = [[NSMutableParagraphStyle defaultParagraphStyle] mutableCopy];
            paragraphStyle1.lineBreakMode = self.titleLabel.lineBreakMode;
            
            CGSize titleSize = [self.titleLabel.text boundingRectWithSize:CGSizeMake(labelMaxWidth,self.titleLabel.font.lineHeight)
                                     options:NSStringDrawingUsesLineFragmentOrigin
                                  attributes:@{NSFontAttributeName: self.titleLabel.font,
                                               NSParagraphStyleAttributeName:paragraphStyle1}
                                     context:nil].size;

    public by mirage3d modified Mar 16, 2016  4176  1  5  0

    Get localised country list from Cocoa in Objective-C

    NSMutableArray *countryNamesArray = [NSMutableArray array];
    NSLocale *currentLocale = [NSLocale currentLocale];
    for (NSString *countryCode in [NSLocale ISOCountryCodes]) {
        [countryNamesArray addObject:[currentLocale displayNameForKey:NSLocaleCountryCode value:countryCode]];
    }
    

    public by marksimon232 modified Jul 3, 2014  2845  1  6  2

    Swift Collections: Writing an IF statement

    Objective-C to Swift
    Objective-C:
    Notice in the second example that the If statement tests if the variable “aString” is empty.
    
    int score = 25;
    if (score > 25) {
    }
     
    NSString *aString;
    if (aString) {
    }
    
    ==========THIS CODE WILL BE WRITTEN AS==============
    
    Swift:
    In Swift, we need to denote that a variable has an optional value with a question mark. This means that it could be empty.
    Then we use the let keyword and a name to test if aString is empty.
    
    var score = 25
    if score > 25 {
    }
     
    var aString: String?
    if let myString = aString {
    }
    • Public Snippets
    • Channels Snippets