OC底层 - runtime 一个char变量按位保存多个BOOL变量值

在arm64之后, 优化了isa指针实现, 使用共同体实现isa, 可以节省内存:

以下使用一个char变量保存多个BOOL变量的值, 实现代码:

///左移操作
#define LPTail (1<<0)
#define LPRich (1<<1)
#define LPHandsome (1<<2)
//
//  Person.m
//  demo007_runtime_isa_共用体
//
//  Created by Batac on 2020/12/7.
//




#import "Person.h"


@interface Person()
{
    //一个字节   可以表示8个BOOL变量
    char _tailRichHandsome;
}
@end

@implementation Person

-(BOOL)getTail{
    return !!(_tailRichHandsome & LPTail);
}

-(void)setTail:(BOOL)tail{
    if (tail) {
        _tailRichHandsome = _tailRichHandsome | LPTail;
    }else{
        _tailRichHandsome = _tailRichHandsome & ~LPTail;
    }
}

-(BOOL)getRich{
    return !!(_tailRichHandsome & LPRich);
}

-(void)setRich:(BOOL)rich{
    if (rich) {
        _tailRichHandsome = _tailRichHandsome | LPRich;
    }else{
        _tailRichHandsome = _tailRichHandsome & ~LPRich;
    }
}

-(BOOL)getHandsome{
    return !!(_tailRichHandsome & LPHandsome);
}

-(void)setHandsome:(BOOL)handsome{
    if (handsome) {
        _tailRichHandsome = _tailRichHandsome | LPHandsome;
    }else{
        _tailRichHandsome = _tailRichHandsome & ~LPHandsome;
    }
}

@end

在WPF中,如果你想要将一个控件的布尔属性关联到多个布尔变量,你可以使用数据绑定和值转换器来实现。这种方式可以让你根据多个变量的值动态地改变控件的属性。具体步骤如下: 1. 创建一个值转换器(IValueConverter):这个转换器会接收多个布尔值作为输入,并返回一个布尔值给控件属性。这个转换器需要实现IValueConverter接口。 ```csharp using System; using System.Globalization; using System.Windows.Data; public class MultiBoolToBoolConverter : IValueConverter { public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture) { // 你可以在这里编写逻辑来决定如何根据多个值来返回一个布尔值 bool result = true; foreach (var value in values) { if (value is bool && !(bool)value) { result = false; break; } } return result; } public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) { throw new NotImplementedException(); } } ``` 2. 在XAML中声明这个转换器资源: ```xml <Window.Resources> <local:MultiBoolToBoolConverter x:Key="MultiBoolToBoolConverter"/> </Window.Resources> ``` 3. 将控件的属性绑定到一个辅助的布尔属性,并使用MultiBinding来绑定多个值: ```xml <CheckBox IsChecked="{Binding Path=CombinedBool, Converter={StaticResource MultiBoolToBoolConverter}, ConverterParameter=your_parameter}"> <CheckBox.IsEnabled> <MultiBinding Converter="{StaticResource MultiBoolToBoolConverter}"> <Binding Path="BoolProperty1"/> <Binding Path="BoolProperty2"/> <!-- 更多的布尔属性 --> </MultiBinding> </CheckBox.IsEnabled> </CheckBox> ``` 在这个例子中,`CombinedBool`是你的后端代码中的一个布尔属性,它会根据`BoolProperty1`和`BoolProperty2`等属性的值通过转换器来决定其值。`MultiBinding`允许你根据多个源属性的值来决定目标属性的值。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值