[英]Access an Objective C dictionary from an Objective C ViewController in a Swift ViewController
I have a project in Swift in which I added a ViewController written in Objective C that ranges all beacons that it finds in his range (didn't find a swift code for this - this is why I'm using Obj C).我在 Swift 中有一个项目,其中我添加了一个用 Objective C 编写的 ViewController,它涵盖了它在他的范围内找到的所有信标(没有找到 Z818056DBD7E201243206B9C7CD88481C 代码) I connected the two ViewControllers and all is good, the beacons are displayed on the screen of the Objective C ViewController.
我连接了两个 ViewController,一切都很好,信标显示在 Objective C ViewController 的屏幕上。 The problem is that I need to access, from the Objective C ViewController, a dictionary ("beaconsDict"), that is created in there, so I can pass all beacons RSSIs to a BLE gateway afterwards.
问题是我需要从 Objective C ViewController 访问在其中创建的字典(“beaconsDict”),因此我可以在之后将所有信标 RSSI 传递给 BLE 网关。 The problem is that I don't know how to read/access from the Swift ViewController the Dictionary from the Obj C ViewController so I can create a list of RSSIs which, afterwards, I will pass to the BLE Gateway.
问题是我不知道如何从 Swift ViewController 读取/访问 Obj C ViewController 中的字典,因此我可以创建一个 RSSI 列表,之后我将传递给 BLE 网关。
(the bridging is done and as I mentioned the Obj C ViewController is visible in my application, at a push of a button, and works as expected) (桥接完成,正如我提到的,Obj C ViewController 在我的应用程序中可见,只需按一下按钮,就可以按预期工作)
Obj C ViewController m file: Obj C ViewController m 文件:
#import "AIBMainTableViewController.h"
#import "AIBBeaconRegionAny.h"
#import "AIBUtils.h"
#import "AIBDetailViewController.h"
#define kCellIdentifier @"cellBeaconIdentifier"
@import CoreLocation;
@interface AIBMainTableViewController ()<CLLocationManagerDelegate>
@property(nonatomic, strong) NSDictionary* beaconsDict;
@property(nonatomic, strong) CLLocationManager* locationManager;
@property(nonatomic, strong) NSArray* listUUID;
@property(nonatomic) BOOL sortByMajorMinor;
@property(nonatomic, retain) CLBeacon* selectedBeacon;
@end
@implementation AIBMainTableViewController
- (void)viewDidLoad
{
[super viewDidLoad];
self.clearsSelectionOnViewWillAppear = NO;
self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.delegate = self;
self.listUUID=[[NSArray alloc] init];
self.beaconsDict=[[NSMutableDictionary alloc] init];
self.sortByMajorMinor=NO;
AIBBeaconRegionAny *beaconRegionAny = [[AIBBeaconRegionAny alloc] initWithIdentifier:@"Any"];
[self.locationManager requestWhenInUseAuthorization];
[self.locationManager startRangingBeaconsInRegion:beaconRegionAny];
self.navigationItem.rightBarButtonItem=[[UIBarButtonItem alloc] initWithTitle:@"Order by major/minor" style:UIBarButtonItemStylePlain target:self action:@selector(changeOrdenation)];
}
- (void) changeOrdenation{
_sortByMajorMinor=!_sortByMajorMinor;
if (_sortByMajorMinor){
self.navigationItem.rightBarButtonItem.title=@"Order by distance";
} else {
self.navigationItem.rightBarButtonItem.title=@"Order by major/minor";
}
}
- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status
{
NSLog(@"locationManagerDidChangeAuthorizationStatus: %d", status);
[UIAlertController alertControllerWithTitle:@"Authoritzation Status changed"
message:[[NSString alloc] initWithFormat:@"Location Manager did change authorization status to: %d", status]
preferredStyle:UIAlertControllerStyleAlert];
}
- (void)locationManager:(CLLocationManager *)manager didRangeBeacons:(NSArray *)beacons inRegion:(CLBeaconRegion *)region
{
NSLog(@"locationManager:%@ didRangeBeacons:%@ inRegion:%@",manager, beacons, region);
NSMutableArray* listUuid=[[NSMutableArray alloc] init];
NSMutableDictionary* beaconsDict=[[NSMutableDictionary alloc] init];
for (CLBeacon* beacon in beacons) {
NSLog(@"rssi is : %ld", beacon.rssi);
NSString* uuid=[beacon.proximityUUID UUIDString];
NSMutableArray* list=[beaconsDict objectForKey:uuid];
if (list==nil){
list=[[NSMutableArray alloc] init];
[listUuid addObject:uuid];
[beaconsDict setObject:list forKey:uuid];
}
[list addObject:beacon];
}
[listUuid sortUsingComparator:^NSComparisonResult(id obj1, id obj2) {
NSString* string1=obj1;
NSString* string2=obj2;
return [string1 compare:string2];
}];
if (_sortByMajorMinor){
for (NSString* uuid in listUuid){
NSMutableArray* list=[beaconsDict objectForKey:uuid];
[list sortUsingComparator:^NSComparisonResult(id obj1, id obj2) {
CLBeacon* b1=obj1;
CLBeacon* b2=obj2;
NSComparisonResult r=[b1.major compare:b2.major];
if (r==NSOrderedSame){
r=[b1.minor compare:b2.minor];
}
return r;
}];
}
}
_listUUID=listUuid;
_beaconsDict=beaconsDict;
[self.tableView reloadData];
}
- (void)locationManager:(CLLocationManager *)manager rangingBeaconsDidFailForRegion:(CLBeaconRegion *)region withError:(NSError *)error
{
NSLog(@"locationManager:%@ rangingBeaconsDidFailForRegion:%@ withError:%@", manager, region, error);
[UIAlertController alertControllerWithTitle:@"Ranging Beacons fail"
message:[[NSString alloc] initWithFormat:@"Ranging beacons fail with error: %@", error]
preferredStyle:UIAlertControllerStyleAlert];
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return [_listUUID count];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
NSString* key=[_listUUID objectAtIndex:section];
return [[_beaconsDict objectForKey:key] count];
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
return [_listUUID objectAtIndex:section];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kCellIdentifier forIndexPath:indexPath];
NSString* key=[_listUUID objectAtIndex:[indexPath indexAtPosition:0]];
CLBeacon* beacon=[[_beaconsDict objectForKey:key] objectAtIndex:[indexPath indexAtPosition:1]];
cell.textLabel.text=[[NSString alloc] initWithFormat:@"RSSI:%ld", (long)beacon.rssi];
cell.detailTextLabel.text=[[NSString alloc] initWithFormat:@"Distance: %.2fm\tProximity: %@", beacon.accuracy, [AIBUtils stringForProximityValue:beacon.proximity]];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
NSString* key=[_listUUID objectAtIndex:[indexPath indexAtPosition:0]];
_selectedBeacon=[[_beaconsDict objectForKey:key] objectAtIndex:[indexPath indexAtPosition:1]];
AIBDetailViewController* detail=[self.storyboard instantiateViewControllerWithIdentifier:@"detail"];
detail.beacon=_selectedBeacon;
[self.navigationController pushViewController:detail animated:YES];
}
@end
Obj C ViewController h file: Obj C ViewController h 文件:
#import <UIKit/UIKit.h>
@interface AIBMainTableViewController : UITableViewController
@end
Approach 1:方法一:
Declare NSDictionary *dictionaryBeacons;
声明
NSDictionary *dictionaryBeacons;
in your.h file.在你的.h 文件中。 Initialization and value assignment will happen in.m
初始化和赋值将发生在.m
Then in.swift file you can declare one property like var dictBeacons: [String:Any]?然后在.swift 文件中,您可以声明一个属性,如 var dictBeacons: [String:Any]?
Now in.m file you can create object of swift view controller and pass dict as parameter like现在在.m 文件中,您可以创建 object 的 swift 视图 controller 并将 dict 作为参数传递
vcObject.dictBeacons = dictionaryBeacons;
and then PUSH it using navigation controller.然后使用导航 controller 推送它。
Approach 2:方法二:
If you don't want PUSH controller and use it in random.swift class then Declare @property(nonatomic, strong) NSDictionary *dictionaryBeacons;
如果您不想 PUSH controller 并随机使用它。swift class 然后声明 @property
@property(nonatomic, strong) NSDictionary *dictionaryBeacons;
in your.h file and use it any.swift class by creating object of VC like vcObject.dictionaryBeacons
在 your.h 文件中并使用它 any.swift class 通过创建像 vcObject.dictionaryBeacons 这样的 VC 的
vcObject.dictionaryBeacons
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.