iOS零碎知識點<初級版>
iOS零碎知識點<中階版>
iOS零碎知識點<高階版>
iOS零碎知識點<工具篇>
獲取屬性列表
unsigned int count = 0;
Ivar *members = class_copyIvarList([obj class], &count);
for (int i = 0 ; i < count; i++) {
Ivar var = members[i];
//獲取變量名稱
const char *memberName = ivar_getName(var);
//獲取變量類型
const char *memberType = ivar_getTypeEncoding(var);
NSLog(@"%s----%s", memberName, memberType);
}
//重新給屬性賦值
//"_callDelegate" 為屬性名
Ivar var = class_getInstanceVariable([obj class], "_callDelegate");
object_setIvar(obj, var, self);
獲取類的所有屬性
/**
* 獲取類的所有屬性
*/
+(void)showStudentClassProperties
{
Class cls = [Student class];
unsigned int count;
while (cls!=[NSObject class]) {
objc_property_t *properties = class_copyPropertyList(cls, &count);
for (int i = 0; i<count; i++) {
objc_property_t property = properties[i];
NSString *propertyName = [[NSString alloc] initWithCString:property_getName(property) encoding:NSUTF8StringEncoding];
NSLog(@"屬性名==%@",propertyName);
}
if (properties){
//要釋放
free(properties);
}
//得到父類的信息
cls = class_getSuperclass(cls);
}
}
獲取類的所有方法
/**
* 獲取類的所有方法
*/
+(void)showStudentClassMethods
{
unsigned int count;
Class cls = [Student class];
while (cls!=[NSObject class]){
Method *methods = class_copyMethodList(cls, &count);
for (int i=0; i < count; i++) {
NSString *methodName = [NSString stringWithCString:sel_getName(method_getName(methods[i])) encoding:NSUTF8StringEncoding];
NSLog(@"方法名:%@ ", methodName);
}
if (methods) {
free(methods);
}
cls = class_getSuperclass(cls);
}
}
獲取類的所有屬性 做健值反射
/**
* 獲取類的所有屬性 做健值反射
*/
+(Student *)showStudentClassPropertiesToMapValueWithDic:(NSDictionary *)dic
{
Student *stu = [[Student alloc] init];
Class cls = [Student class];
unsigned int count;
while (cls!=[NSObject class]) {
objc_property_t *properties = class_copyPropertyList(cls, &count);
for (int i = 0; i<count; i++) {
objc_property_t property = properties[i];
NSString *propertyName = [[NSString alloc] initWithCString:property_getName(property) encoding:NSUTF8StringEncoding];
//得到屬性名可以在這里做反射 反饋過來的dic 直接反射成一個model
/**
* valueForKey: 是 KVC(NSKeyValueCoding) 的方法,在 KVC 里可以通過 property 同名字符串來獲取對應的值
* 這里的dic 的key 與 stu 的屬性名一一對應
* (MVC模式設計數據反射時候用到{類定義屬性時候要和服務端反饋過來的字段一樣})
*/
id propertyValue = [dic valueForKey:propertyName];
if (propertyValue)
[stu setValue:propertyValue forKey:propertyName];//屬性get set賦值
// NSLog(@"%@ = %@",propertyName,propertyValue);
}
if (properties){
free(properties);
}
//得到父類的信息
cls = class_getSuperclass(cls);
}
return stu;
}
判別這個屬性是否具備get set方法
// 判別這個屬性是否具備get set方法 (重要血当!以上都要添加這個判別)
NSString *propertySetMethod = [NSString stringWithFormat:@"set%@%@:", [[propertyName substringToIndex:1] capitalizedString] ,[propertyName substringFromIndex:1]];
SEL selector = NSSelectorFromString(propertySetMethod);
if ([model respondsToSelector:selector])
{
[model setValue:propertyValue forKey:propertyName];
}
高寬比例計算方法及等比高寬修改計算方法
假設:
高=G,寬=K,比例=B;
比例公式:
B= G / K;
等比修正公式:
如果改變寬度(K),則高度(G)計算公式應為:K * B;
如果改變高度(G)油宜,則寬度(K)計算公式應為:G / B;
代入值進行計算:
Size = 1024; (默認縮放值)
G = 111, K = 370;
B = G / K; (雙精度浮點數)
修改寬度
K = 1024, G = K * B; (四舍五入取整)
修改高度
G = 1024, K = G / B; (四舍五入取整)
書籍寬高計算比例:
- (void)scale {
NSInteger width = 1280;
NSInteger height = 720;
NSInteger sacle = getScale(width, height);
NSLog(@"%ld :%ld",width / sacle, height / sacle);
}
NSInteger getScale(NSInteger w, NSInteger h) {
if (w % h) {
return gcd(h, w % h);
} else {
return h;
}
}
判定是否設置了網絡代理
需要導入框架CFNetwork另伍,然后宪睹,這個方法是MRC的:需要添加
-fno-objc-arc
的flag,代碼如下:
+ (BOOL)getProxyStatus {
NSDictionary *proxySettings = NSMakeCollectable([(NSDictionary*)CFNetworkCopySystemProxySettings() autorelease]);
NSArray *proxies = NSMakeCollectable([(NSArray*)CFNetworkCopyProxiesForURL((CFURLRef)[NSURL URLWithString:@"http://www.google.com"], (CFDictionaryRef)proxySettings) autorelease]);
NSDictionary *settings = [proxies objectAtIndex:0];
NSLog(@"host=%@", [settings objectForKey:(NSString*)kCFProxyHostNameKey]);
NSLog(@"port=%@", [settings objectForKey:(NSString*)kCFProxyPortNumberKey]);
NSLog(@"type=%@", [settings objectForKey:(NSString*)kCFProxyTypeKey]);
if ([[settings objectForKey:(NSString *)kCFProxyTypeKey] isEqualToString:@"kCFProxyTypeNone"])
{
//沒有設置代理
return NO;
} else {
//設置代理了
return YES;
}
}
漢字轉拼音
+ (NSString *)transform:(NSString *)chinese
{
//將NSString裝換成NSMutableString
NSMutableString *pinyin = [chinese mutableCopy];
//將漢字轉換為拼音(帶音標)
CFStringTransform((__bridgeCFMutableStringRef)pinyin, NULL, kCFStringTransformMandarinLatin, NO);
NSLog(@"%@", pinyin);
//去掉拼音的音標
CFStringTransform((__bridgeCFMutableStringRef)pinyin, NULL, kCFStringTransformStripCombiningMarks, NO);
NSLog(@"%@", pinyin);
//返回最近結果
return pinyin;
}
- (WKWebView *)wk_WebView{
if (!_wk_WebView) {
// 禁止選擇CSS
NSString *css = @"body{-webkit-user-select:none;-webkit-user-drag:none;}";
// CSS選中樣式取消
NSMutableString *javascript = [NSMutableString string];
[javascript appendString:@"var style = document.createElement('style');"];
[javascript appendString:@"style.type = 'text/css';"];
[javascript appendFormat:@"var cssContent = document.createTextNode('%@');", css];
[javascript appendString:@"style.appendChild(cssContent);"];
[javascript appendString:@"document.body.appendChild(style);"];
// javascript注入
WKUserScript *noneSelectScript = [[WKUserScript alloc] initWithSource:javascript injectionTime:WKUserScriptInjectionTimeAtDocumentEnd forMainFrameOnly:YES];
WKUserContentController *userContentController = [[WKUserContentController alloc] init];
[userContentController addUserScript:noneSelectScript];
WKWebViewConfiguration *config = [[WKWebViewConfiguration alloc] init];
config.preferences = [[WKPreferences alloc] init];
config.userContentController = userContentController;
[config.userContentController addScriptMessageHandler:self name:ScriptMessageHandlerName];
// [_wk_WebView evaluateJavaScript:ScriptMessageHandlerName completionHandler:^(id _Nullable response, NSError * _Nullable error) {
// NSLog(@"response: %@ error: %@", response, error);
// NSLog(@"call js alert by native");
// }];
_wk_WebView = [[WKWebView alloc] initWithFrame:self.view.bounds
configuration:config];
_wk_WebView.UIDelegate = self;
_wk_WebView.navigationDelegate = self;
_wk_WebView.scrollView.bounces = NO;
_wk_WebView.scrollView.contentInset = UIEdgeInsetsMake(0, 0, kNavgationHeight, 0);
//側滑
_wk_WebView.allowsBackForwardNavigationGestures = YES;
if (IOS_VERSION_10_OR_LATER) {
_wk_WebView.scrollView.refreshControl = self.refreshControl;
}
// 添加KVO監(jiān)聽 //進度
[_wk_WebView addObserver:self forKeyPath:ObserveWebKey_Progress options:NSKeyValueObservingOptionNew context:NULL];
}
return _wk_WebView;
}
獲取CPU核數
unsigned int countOfCores()
{
unsigned int ncpu;
size_t len = sizeof(ncpu);
sysctlbyname("hw.ncpu", &ncpu, &len, NULL, 0);
return ncpu;
}