Friday, December 18, 2020
Explain how bitcode works
Thursday, December 17, 2020
Wednesday, December 2, 2020
How to create abstract functions in Swift
class BaseClass {
func abstractFunction() {
preconditionFailure("This method must be overridden")
}
}
class SubClass : BaseClass {
override func abstractFunction() {
// Override
}
}
Wednesday, November 18, 2020
Get Value For Selector
func getValueForSelector(cls: AnyClass, selector: Selector) -> AnyObject {
let method = class_getInstanceMethod((cls), selector)!
let methodIMP : IMP! = method_getImplementation(method)
let newAmountObj = unsafeBitCast(methodIMP,to:(@convention(c)(AnyClass?,Selector,Any?)->Any).self)(cls,selector, nil) as AnyObject
return newAmountObj
}
Accessor Search Patterns
Search Pattern for the Basic Setter
The default implementation of setValue:forKey:, given key and value parameters as input, attempts to set a property named key to value (or, for non-object properties, the unwrapped version of value, as described in Representing Non-Object Values) inside the object receiving the call, using the following procedure:
Look for the first accessor named
set<Key>:or_set<Key>, in that order. If found, invoke it with the input value (or unwrapped value, as needed) and finish.If no simple accessor is found, and if the class method
accessInstanceVariablesDirectlyreturnsYES, look for an instance variable with a name like_<key>,_is<Key>,<key>, oris<Key>, in that order. If found, set the variable directly with the input value (or unwrapped value) and finish.Upon finding no accessor or instance variable, invoke
setValue:forUndefinedKey:. This raises an exception by default, but a subclass ofNSObjectmay provide key-specific behavior.
Tuesday, November 17, 2020
3 ways to call ObjC methods from swift
There are three ways to dynamically call the method in this class:
1. Using performSelector()
let selector = NSSelectorFromString("titleForItem:withTag:")
let unmanaged = toolbar.perform(selector, with: "foo", with: "bar")
let result = unmanaged?.takeRetainedValue() as? String
2. Using methodForSelector() with @convention(c)
typealias titleForItemMethod = @convention(c)
(NSObject, Selector, NSString, NSString) -> NSString
let selector = NSSelectorFromString("titleForItem:withTag:")
let methodIMP = toolbar.method(for: selector)
let method = unsafeBitCast(methodIMP, to: titleForItemMethod.self)
let result = method(toolbar, selector, "foo", "bar")
3. Using NSInvocation
It's only available in Objective-C.
SEL selector = @selector(titleForItem:withTag:);
NSMethodSignature *signature = [toolbar methodSignatureForSelector:selector];
NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:signature];
invocation.target = toolbar;
invocation.selector = selector;
NSString *argument1 = @"foo";
NSString *argument2 = @"bar";
[invocation setArgument:&argument1 atIndex:2];
[invocation setArgument:&argument2 atIndex:3];
[invocation invoke];
NSString *result;
[invocation getReturnValue:&result];
Or, we can use Dynamic 🎉
let result = Dynamic(toolbar) // Wrap the object with Dynamic
.titleForItem("foo", withTag: "bar") // Call the method directly!
More details on how the library is designed and how it works here.
performSelector()let selector = NSSelectorFromString("titleForItem:withTag:")
let unmanaged = toolbar.perform(selector, with: "foo", with: "bar")
let result = unmanaged?.takeRetainedValue() as? StringmethodForSelector() with @convention(c)typealias titleForItemMethod = @convention(c)
(NSObject, Selector, NSString, NSString) -> NSString
let selector = NSSelectorFromString("titleForItem:withTag:")
let methodIMP = toolbar.method(for: selector)
let method = unsafeBitCast(methodIMP, to: titleForItemMethod.self)
let result = method(toolbar, selector, "foo", "bar")NSInvocationIt's only available in Objective-C.
SEL selector = @selector(titleForItem:withTag:);
NSMethodSignature *signature = [toolbar methodSignatureForSelector:selector];
NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:signature];
invocation.target = toolbar;
invocation.selector = selector;
NSString *argument1 = @"foo";
NSString *argument2 = @"bar";
[invocation setArgument:&argument1 atIndex:2];
[invocation setArgument:&argument2 atIndex:3];
[invocation invoke];
NSString *result;
[invocation getReturnValue:&result];let result = Dynamic(toolbar) // Wrap the object with Dynamic
.titleForItem("foo", withTag: "bar") // Call the method directly!More details on how the library is designed and how it works here.
Calling selectors using Dynamic library
let result = Dynamic(toolbar) // Wrap the object with Dynamic
.titleForItem("foo", withTag: "bar") // Call the method directly!https://github.com/mhdhejazi/Dynamic
https://github.com/mhdhejazi/Dynamic/blob/master/Tests/DynamicTests/DynamicTests.swift
Friday, November 13, 2020
Wednesday, November 11, 2020
How to call methods using selectors
Dealing with managed and unmanaged objects
While most CoreFoundation APIs have been annotated, some significant chunks have yet to receive attention. As of this writing, the Address Book framework seems the highest profile of the unannotated APIs, with several functions taking or returning Unmanaged-wrapped types.
An Unmanaged<T> instance wraps a CoreFoundation type T, preserving a reference to the underlying object as long as the Unmanaged instance itself is in scope. There are two ways to get a Swift-managed value out of an Unmanaged instance:
take: returns a Swift-managed reference to the wrapped instance, decrementing the reference count while doing so—use with the return value of a Create Rule function.Retained Value()
take: returns a Swift-managed reference to the wrapped instance without decrementing the reference count—use with the return value of a Get Rule function.Unretained Value()
https://nshipster.com/unmanaged/
converting an unmanaged object to a string
https://vandadnp.wordpress.com/2014/07/07/swift-convert-unmanaged-to-string/
Monday, November 2, 2020
how to get a certificate in java
http://littlesvr.ca/grumble/2014/07/21/android-programming-connect-to-an-https-server-with-self-signed-certificate/
Sunday, November 1, 2020
Merge dictionaries in Swift
dictionary1.merge(dictionary2) {(current,_) in current} |
Keep the new value (value in dictionary2) if there is a duplicate key
dictionary1.merge(dictionary2) {(_,new) in new} |
https://www.tutorialkart.com/swift-tutorial/merge-dictionaries-in-swift/
Monday, August 3, 2020
How to reset the scroll back buffer on a Mac terminal via command line
printf '\033[2J\033[3J\033[1;1H'Tuesday, June 2, 2020
Wednesday, May 27, 2020
c++ switch Vs hash map optimization
- cost(Hash_table) >> cost(direct_lookup_table)
- cost(direct_lookup_table) ~= cost(switch) if your compiler translates switches into lookup tables.
- cost(switch) >> cost(direct_lookup_table) (O(N) vs O(1)) if your compiler does not translate switches and use conditionals, but I can't think of any compiler doing this.
- But inlined direct threading makes the code less readable.