Thursday, June 23, 2022

Which type of encryption you have used in iOS App?

Ans : I have used trippleDES for passing data in request to server.


NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dicPostDict options:kNilOptions error:&error];

 NSString *strJsonData=[[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];

 NSData *encryptedJsonString = [CommonMethods tripleDesEncryptString:strJsonData  key:@"2-055PL&okjnnhkey@inihgr" error:nil];
 NSString *strencryptedJsonString = [encryptedJsonString base64Encoding];

What is MD5 hash in Swift?

Thursday, May 12, 2022

What are the differences between throws and rethrows in Swift?

Rethrowing Functions and Methods

                A function or method can be declared with the rethrows keyword to indicate that it throws an error only if one of it’s function parameters throws an error. These functions and methods are known as rethrowing functions and rethrowing methods. Rethrowing functions and methods must have at least one throwing function parameter.

In our understanding way as below:

Roughly speaking, rethrows is for functions which do not throw errors "on their own", but only "forward" errors from their function parameters.

Example:

In your case,


func throwCustomError(function:(String) throws -> ()) throws


denotes a function which can throw an error, even if called with a non-throwing argument, whereas



-------


func rethrowCustomError(function:(String) throws -> ()) rethrows


denotes a function which throws an error only if called with a throwing argument.


Roughly speaking, rethrows is for functions which do not throw errors "on their own", but only "forward" errors from their function parameters.

Tuesday, April 19, 2022

Delete Rules of Core data

Core Data Delete Rules:


1. No Action :
    When source is deleted, but destination won't be notified.

2. Nullify : [It is default one by core data]
    When source is deleted, but the relationship is nullified, won't effect on destination.

3. Cascade : [Not safe]
    When source is deleted, and it also sent to delete the destination as well
    by cascading info to destination.

4. Deny : [Kind of Safe Check]
    When source is deleted, the destinations won't get any effect.
    And The source will be deleted until and unless its associated
    all destinations will be deleted.
    So it will deny if associated destination still exist.

Tuesday, March 29, 2022

Email Validation Regular Expression

func isValidEmail() -> Bool {

    let emailRegEx = "[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,64}"
    let emailPred = NSPredicate(format:"SELF MATCHES %@", emailRegEx)
    return emailPred.evaluate(with: self.trimmingCharacters(in: .whitespacesAndNewlines))

}

Monday, March 7, 2022

What is Difference between weak and unowned?

Ans:

Both weak and unowned references do not create a strong hold on the referred object (a.k.a. they don't increase the retain count in order to prevent ARC from deallocating the referred object).

But why two keywords? This distinction has to do with the fact that Optional types are built-in the Swift language. Long story short about them: optional types offer memory safety (this works beautifully with Swift's constructor rules - which are strict in order to provide this benefit).

weak reference allows the possibility of it to become nil (this happens automatically when the referenced object is deallocated), therefore the type of your property must be optional - so you, as a programmer, are obligated to check it before you use it (basically the compiler forces you, as much as it can, to write safe code).

An unowned reference presumes that it will never become nil during its lifetime. An unowned reference must be set during initialization - this means that the reference will be defined as a non-optional type that can be used safely without checks. If somehow the object being referred to is deallocated, then the app will crash when the unowned reference is used.

From the Apple docs:

Use a weak reference whenever it is valid for that reference to become nil at some point during its lifetime. Conversely, use an unowned reference when you know that the reference will never be nil once it has been set during initialization.

In the docs, there are some examples that discuss retain cycles and how to break them. All these examples are extracted from the docs.

Example of the weak keyword:

class Person {
    let name: String
    init(name: String) { self.name = name }
    var apartment: Apartment?
}
 
class Apartment {
    let number: Int
    init(number: Int) { self.number = number }
    weak var tenant: Person?
}

And now, for some ASCII art (you should go see the docs - they have pretty diagrams):

Person ===(strong)==> Apartment
Person <==(weak)===== Apartment

The Person and Apartment example shows a situation where two properties, both of which are allowed to be nil, have the potential to cause a strong reference cycle. This scenario is best resolved with a weak reference. Both entities can exist without having a strict dependency upon the other.

Example of the unowned keyword:

class Customer {
    let name: String
    var card: CreditCard?
    init(name: String) { self.name = name }
}
 
class CreditCard {
    let number: UInt64
    unowned let customer: Customer
    init(number: UInt64, customer: Customer) { self.number = number; self.customer = customer }
}

In this example, a Customer may or may not have a CreditCard, but a CreditCard will always be associated with a Customer. To represent this, the Customer class has an optional card property, but the CreditCard class has a non-optional (and unowned) customer property.

Customer ===(strong)==> CreditCard
Customer <==(unowned)== CreditCard

The Customer and CreditCard example shows a situation where one property that is allowed to be nil and another property that cannot be nil has the potential to cause a strong reference cycle. This scenario is best resolved with an unowned reference.

Note from Apple:

Weak references must be declared as variables, to indicate that their value can change at runtime. A weak reference cannot be declared as a constant.

There is also a third scenario when both properties should always have a value, and neither property should ever be nil once initialization is complete.

And there are also the classic retain cycle scenarios to avoid when working with closures.

For this, I encourage you to visit the Apple docs, or read the book.

Wednesday, March 2, 2022

Operation Queue

1. What is Operation Queue?
2. Difference between Operation Queue and Dispatch Framework?
3. When should you consider using Operation Queue over GCD?
4. Difference between Operation and Operation Queue?