当前位置:网站首页>开发中使用的语言技巧
开发中使用的语言技巧
2022-06-12 13:26:00 【Li.CQ】
- result 的值变化范围是 0 - valueRange / 2, valueRange / 2 - 0, 0 - valueRange / 2, valueRange / 2 - 0;
static int rote = 0;
int valueRange = 20;
int maxValue = valueRange / 2;
int a = rote % valueRange;
int b = rote / maxValue;
int c = b % 2;
int result = (valueRange * c + a * (pow(-1, c)));
rote++;
- result 的值变化范围是 0 - valueRange, 0 - valueRange, 0 - valueRange, 0 - valueRange;
static int rote = 0;
int valueRange = 20;
int result = (rote % valueRange );
rote++;
- scorllView的子view的布局
NSArray <UIView *> * viewsArray;
UIScrollView *scrollView;
__block UIView *lastView = scrollView;
[viewsArray enumerateObjectsUsingBlock:^(UIView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
[obj mas_makeConstraints:^(MASConstraintMaker *make) {
MASViewAttribute * mas_ori = (idx == 0) ? lastView.mas_left : lastView.mas_right;
make.left.equalTo(mas_ori).offset(5);
make.top.bottom.equalTo(scrollView);
if (idx == viewsArray.count - 1) make.right.equalTo(scrollView);
}];
lastView = obj;
}];
// - 返回 大于 wid 的 最小的16的倍数
#define aw_stride(wid) ((wid % 16 != 0) ? ((wid) + 16 - (wid) % 16): (wid))
空判断
// - 判断v1是否等于v2前需要判断v1和v2是否为空.
private boolean valEquals(v1, v2){
return v1 == null ? v2 == null : v1.equals(v2);
}
// - 判断当前的节点是不是左子节点.
boolean left = parent.left == null || node.isLeftChild();
字符串计算hash值
- jack由四个字符组成
- j * n^3 + a * n^2 + c * n^1 + j * n^1; 即: [(j * n + a) * n +c] * n + k j, a, c, k 都有自己的ascaii码, 可以计算出一个整数值
- 在JDK中, n = 31;
- 31是一个奇素数, 31 * i = (( i << 5 ) - i ); 这样 再次进行替换, 就可以提高计算速度.
- JDK中计算hash值
- 可以使用 1 << 4 表示2^4.
String string = "jack";
int len = string.length();
int hashCode = 0;
for (int i = 0; i< len; i++){
char c = string.cahrAt(i);
hashCode = hashCode * 31 + c;
// - 等价于: hashCode = (hashCode << 5) - hashCode + c;
}
a++和 ++a
a++: 先用后加 是先使用a的值,然后再对a做加1处理;
int a = 0;
for (int i = 0; i < 10000; i++){
[array addObject:@(i)];
}
array[a++] = @"abc";
// - 这里结果 array[0] == @"abc", a = 1
++a: 先加后用 是先对a作加1处理,然后再使用a的值。
int a = 0;
for (int i = 0; i < 10000; i++){
[array addObject:@(i)];
}
array[++a] = @"abc";
// - 这里结果 array[1] == @"abc", a = 1
使用表达式做为下标
// - 此时不但改变了a的值, 并且取到了数组的下标为 4 的元素.
int a = 0;
[self.settingModelArr objectAtIndex:(a = 4)];
使用表达式做为判断条件
// - 此时不但改变了a的值, 并且取到了数组的下标为 4 的元素.
while ((node = node.parent) != null) {}
左右交替的情况(先从后到前遍历, 如果不满足条件, 就从前到后遍历, 如果又不满足条件了, 又从后到前遍历, 然后如果又不满足条件在从前到后遍历, 一直循环…)
while (begin < end) {
while (begin < end) {
if (cmp(pivot, array[end]) < 0) { // 右边元素 > 轴点元素
end--;
} else { // 右边元素 <= 轴点元素
array[begin++] = array[end];
break;
}
}
while (begin < end) {
if (cmp(pivot, array[begin]) > 0) { // 左边元素 < 轴点元素
begin++;
} else { // 左边元素 >= 轴点元素
array[end--] = array[begin];
break;
}
}
}
跳出for循环的方式
// - 1. 这个方式退出for 循环 , 很方便
for (NSUInteger i = 0 ; i < 10; i++) {
for (NSUInteger j = 0 ; j < 10; j++) {
if (j == 3 && i == 2) {
goto exitLoop;
}
NSLog(@"i : %lu, j : %lu", (unsigned long)i, j);
}
}
exitLoop:
NSLog(@"exitLoop");
// - 2. 这个方式退出for 循环 , 很方便
bool flag = true;
for (int i = 0; i < 5 && flag == true; i++){
for (int j = 0; j < 5 && flag == true; j++) {
if (i == 3 && j == 5)
flag = false;
}
}
1 和 0 之间的相互转换
// - 1. a = 0, result = 1, a = 1, result = 0;
int result = 1 - a;
// - 2. iOS特性
int result = ! a;
// -
记录a的变化
__block NSInteger row = -1;
// - 先找第一个未完成的任务, 如果没有找到就找第一个待完成或去完成的任务, 如果都没有选中第一个宝箱
[self.userTaskModel.list enumerateObjectsUsingBlock:^(QIEUserRewardTaskItem * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
if (obj.status == QIERewardGetStatusWaitingGet) {
row = idx;
*stop = YES;
}
if (((obj.status == QIERewardGetStatusWaitingDone) || (obj.status == QIERewardGetStatusGoToDone)) && (row == -1)) {
row = idx;
}
}];
// - 记录result的方式
1. int result = MAX(0, row);
2. int result = row >0 ? row : row++;
3. int result = 0; if(result > 0) result = row;
4. 先定义 __block NSInteger row = self.userTaskModel.list.count, 然后在结尾位置 int result = row % self.userTaskModel.list.count;
5. int result = (abs(row) + row) / 2;
// - 选中指定的view
NSIndexPath *idx = [NSIndexPath indexPathForRow:result inSection:0];
[self collectionView:self.boxCollectionView didSelectItemAtIndexPath:idx];
对2取余的本质
// - 对2取余本质就是看这个数组的二进制结果的最低位是0还是1
/*
6 % 2 = 0b160 & 0b001 = 0;
5 % 2 = 0b101 & 0b001 = 1;
4 % 2 = 0b100 & 0b001 = 0;
3 % 2 = 0b011 & 0b001 = 1;
2 % 2 = 0b010 & 0b001 = 0;
1 % 2 = 0b001 & 0b001 = 1;
*/
int a = 100;
a % 2 等价于 a & 1;
两次for循环, 记录上次值得变化
NSArray *arr1, *arr2, *dp;
for (int i = 1; i <= arr1.count; i++) {
int cur = 0;
for (int j = 1; j <= arr2.count; j++) {
int leftTop = cur;
cur = dp[j];
if (arr1[i - 1] == arr2[j - 1]) {
dp[j] = leftTop + 1;
} else {
dp[j] = MAX(dp[j], dp[j - 1]);
}
}
}
使用do while 实现 goto的功能
-(NSString *)test{
NSInteger errorCode = 0;
do {
if (0) {
errorCode = OKWTransactionErrorPwdEmpty;
break;
}
if (0) {
errorCode = OKWTransactionErrorTxDataEmpty;
break;
}
if (0) {
errorCode = OKWTransactionErrorDecodePwdEmpty;
break;
}
if (1) {
return @"成功"
} while (0);
return @"失败"
}
边栏推荐
- Redis message queue repeated consumption
- Will the next star of PPT for workplace speech be you [perfect summary] at the moment
- Title: Yanghui triangle
- B站分布式KV存储混沌工程实践
- Chrome debugging tool
- Redis消息队列重复消费问题
- Seeking magic square of order n with C language
- A brief introduction to Verilog mode
- Application of bit operation in C language
- Summary of question brushing in leetcode sliding window
猜你喜欢

高通平台开发系列讲解(协议篇)QMI简单介绍及使用方法

Embedded driver design

How to solve the problem of data table query error when SQLite writes the registration function?
![[wechat applet development] Part 1: development tool installation and program configuration](/img/a8/f4dcbde295ba7cf738d878464b3af0.png)
[wechat applet development] Part 1: development tool installation and program configuration

"New continent" of mobile application going to sea

leetcode 47. Permutations II full permutations II (medium)

Innovation training (XI) summary of some bugs in the development process

LeetCode滑动窗口刷题总结

用PyTorch进行语义分割

Installation of pagoda
随机推荐
单向环形链表实现约瑟夫环
字节序数据读写
imagemagick:a gentle introduction to magick++
入门深度学习与机器学习的经验和学习路径
Actual combat | realizing monocular camera ranging by skillfully using pose solution
How to solve the problem of data table query error when SQLite writes the registration function?
Openstack network
创新实训(十)高级界面美化
import torch_ Geometric loads some common datasets
1003: align output
2065: [example 2.2] sum of integers
达梦数据库DM8 windows环境安装
Software construction 03 regular expression
微信web开发者工具使用教程,web开发问题
JVM runtime parameters
2063: [example 1.4] cattle eat grass
Embedded system hardware composition - embedded system hardware architecture
Experience and learning path of introductory deep learning and machine learning
JSP jump problem, unable to display database data, and unable to jump
2061: [example 1.2] trapezoidal area