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监听 //进度
[_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;
}