当前位置:网站首页>开发中使用的语言技巧
开发中使用的语言技巧
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 @"失败"
}
边栏推荐
- AWLive 结构体的使用
- import torch_geometric 加载一些常见数据集
- C语言【23道】经典面试题【下】
- Script引入CDN链接提示net::ERR_FILE_NOT_FOUND问题
- Successfully rated Tencent t3-2, 10000 word parsing
- 实战 | 巧用位姿解算实现单目相机测距
- Overview of embedded system 2- composition and application of embedded system
- Django note 21: querying databases using native SQL
- Actual combat | realizing monocular camera ranging by skillfully using pose solution
- torch_ About the geometric Mini batch
猜你喜欢

干货满满,这些知识你必须拿下

Overview of embedded system 2- composition and application of embedded system

Introduction to application design scheme of intelligent garbage can voice chip, wt588f02b-8s

Application of list and Dict

安装MySQL时出错,照着下面这个链接,做到cmd就不行了

深度学习的多个 loss 是如何平衡的?

Getting to know blob objects

Openstack network

There was an error installing mysql. Follow the link below to CMD

Realization of Joseph Ring with one-way ring linked list
随机推荐
C语言【23道】经典面试题【下】
颜色编码格式介绍
import torch_geometric 加载一些常见数据集
verilog-mode的简要介绍
[embedded] serial communication and its case
[EDA] chip layout design: VLSI layout design using electric
2063: [example 1.4] cattle eat grass
【云原生 | Kubernetes篇】Ingress案例实战
Innovation training (XII) project summary
Chaotic engineering practice of distributed kV storage in station B
One line of code to implement shell if else logic
TCP的“非”可靠性
【云原生 | Kubernetes篇】深入了解Deployment(八)
Deploy opengauss database based on Huawei cloud Kunpeng elastic ECS [Gauss is not a mathematician this time]
Software construction 03 regular expression
JSP jump problem, unable to display database data, and unable to jump
Wechat web developer tools tutorial, web development issues
hudi 键的生成(Key Generation)
1004: character triangle
C#DBHelper_FactoryDB_GetConn