小编Mus*_*rci的帖子

使用反射访问模型类的静态变量

我有一个包含一些静态变量和属性的模型类.在运行时我可以获得属性;

let instance = entity.init()

let mirror = Mirror(reflecting:instance)

var propertyStrings = [String]()

for (propertyName, childMirror) in mirror.children {

}
Run Code Online (Sandbox Code Playgroud)

但我也希望将类的静态变量作为列表.那么如何获取静态变量的名称和值列表呢?这是我的模型类的结构:

class ActionModel: NSObject {

static let kLastModified = "LastModified"
static let kEntityName = "EntityName"
static let kIdentifier = "Id"


var lastModified: Int64
var entityName: String?
var identifier : PrimaryKeyString
Run Code Online (Sandbox Code Playgroud)

reflection mirroring ios swift

7
推荐指数
2
解决办法
1697
查看次数

如何检测蓝牙耳机是否插入IOS 8?

在我的项目中,我AVAudioSession用来检测任何耳机已插入或拔出.但在这种情况下,我无法检测何时插入蓝牙设备.这是我的耳机状态代码.

 - (void)audioRouteChangeListenerCallback:(NSNotification*)notification
    {

    NSDictionary *interuptionDict = notification.userInfo;

    NSInteger routeChangeReason = [[interuptionDict valueForKey:AVAudioSessionRouteChangeReasonKey] integerValue];

    switch (routeChangeReason) {

        case AVAudioSessionRouteChangeReasonNewDeviceAvailable:
            //NSLog(@"AVAudioSessionRouteChangeReasonNewDeviceAvailable");

            NSLog(@"Headphone/Line plugged in");

            [_soundButtonOutlet setImage:[UIImage imageNamed:@"sound-on.png"] forState:UIControlStateNormal];

            _headSetState=YES;

            break;

        case AVAudioSessionRouteChangeReasonOldDeviceUnavailable:
            NSLog(@"AVAudioSessionRouteChangeReasonOldDeviceUnavailable");

            NSLog(@"Headphone/Line was pulled. Stopping player....");

             [_soundButtonOutlet setImage:[UIImage imageNamed:@"sound-off.png"] forState:UIControlStateNormal];
            if(_isPlaying==YES)
            {


            [self.player pause];

            [_audioButtonOutlet setImage:[UIImage imageNamed:@"play.png"] forState:UIControlStateNormal];

            _isPlaying=NO;

            }
            _headSetState=NO;

            break;

        case AVAudioSessionRouteChangeReasonCategoryChange:
            // called at start - also when other audio wants to play
            NSLog(@"AVAudioSessionRouteChangeReasonCategoryChange");


            break;
    }



- (BOOL)isHeadsetPluggedIn

{

    AVAudioSessionRouteDescription* route = [[AVAudioSession sharedInstance] …
Run Code Online (Sandbox Code Playgroud)

bluetooth headset audiosession ios8

4
推荐指数
2
解决办法
5012
查看次数

标签 统计

audiosession ×1

bluetooth ×1

headset ×1

ios ×1

ios8 ×1

mirroring ×1

reflection ×1

swift ×1