Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

_SwiftValue appears in objective-c projects #1147

Closed
webMing opened this issue Jan 11, 2019 · 13 comments · Fixed by #1148
Closed

_SwiftValue appears in objective-c projects #1147

webMing opened this issue Jan 11, 2019 · 13 comments · Fixed by #1148

Comments

@webMing
Copy link

webMing commented Jan 11, 2019

Return _SwiftValue Type In OC

My project language is OC, when I implement this method as bellow, I find ret is a _SwiftValue type and can`t use isEqualToString method. My doubt is that this lib support OC, but why i get a _SwiftValue type value in callback block? Please provide some ideas or reference links.

[socket on:@"statusChange" callback:^(NSArray * _Nonnull arr, SocketAckEmitter * _Nonnull ack) {
    id ret  = [arr firstObject];// when i make a break here,I find ret is _SwiftValue. and it`s _SwfitValue type
    if ([ret isEqualToString:@"connecting"]) {
            //do something usefull;
        }
}];

Connecting,connect ....and other connect event not trigger

[socket on:@"connecting" callback:^(NSArray * _Nonnull arr, SocketAckEmitter * _Nonnull ack) {
        //do something.....
 }];
[socket on:@"connect" callback:^(NSArray * _Nonnull arr, SocketAckEmitter * _Nonnull ack) {
        //do something.....
 }];

I find that connecting, connected... and other connect status event not trigger, but statusChange event is trriger. this doubt me. and this is my code as bellow

- (void)config {

   NSURL* url = [[NSURL alloc] initWithString:SOCKETIOADDR];
    NSDictionary *config = @{@"log":@YES};
    SocketManager* manager = [[SocketManager alloc] initWithSocketURL:url config:config];
    SocketIOClient* socket = manager.defaultSocket;
    _manager = manager;
    _socket = socket;

    /*event type(may error)
     case .connected:    return "connected"
     case .connecting:   return "connecting"
     case .disconnected: return "disconnected"
     case .notConnected: return "notConnected"
     */
    [socket on:@"connecting" callback:^(NSArray * _Nonnull arr, SocketAckEmitter * _Nonnull ack) {
        // when socket is connecting this method not call. the same as connected, disconnected event
    }];
    
    [socket on:@"statusChange" callback:^(NSArray * _Nonnull arr, SocketAckEmitter * _Nonnull ack) {      //when connect status is change, this method called.
        id  swiftValue  = [arr firstObject];
        NSString *statusStr  = swiftValue;
        if ([statusStr isEqualToString:@"connecting"]) {
           
        }
        NSLog(@"---------------------- socket  connect statusChange :%@",[arr firstObject]);
    }];
    
}

- (void)open {
      [_socket connect];
}

- (void)close {
    [_socket disconnect];
    [_manager disconnect];
}

I Integrate this lib use Pod. Thank you in advance..

@nuclearace
Copy link
Member

@webMing Can you not cast the first argument to NSString?

@nuclearace
Copy link
Member

Also, can you post logs of the initial socket connection? It might shed some light on why it's not connecting.

@webMing
Copy link
Author

webMing commented Jan 11, 2019

@webMing Can you not cast the first argument to NSString?
I have do this, but it crash, and i find ret is _Swiftype. i will try again ....

@webMing
Copy link
Author

webMing commented Jan 11, 2019

Also, can you post logs of the initial socket connection? It might shed some light on why it's not connecting.

I will post logs, and it can connect success, but when i Implement [socket on:@"connecting" callback:^{}] method, which para may connecting or connected or other connect status event ,i
find those methods not call.

@nuclearace
Copy link
Member

Ah right, statusChange's data arg is an enum type. So in Objective-C it's going to be an int I believe. But the Swift bridging should also create an Objective-C way of referring to the various cases, I just haven't looked at what they are in ObjC

@webMing
Copy link
Author

webMing commented Jan 11, 2019

Ah right, statusChange's data arg is an enum type. So in Objective-C it's going to be an int I believe. But the Swift bridging should also create an Objective-C way of referring to the various cases, I just haven't looked at what they are in ObjC

###This is test demo in my OC project ,you may have a try .

@import SocketIO;
@interface ViewController ()
@property (strong, nonatomic) SocketManager *manager;
@property (strong, nonatomic) SocketIOClient *socket;
@end
-(void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    [self configure];
}
- (void)configure{
    NSURL* url = [[NSURL alloc] initWithString:@"https://localhost:8080"];
    self.manager = [[SocketManager alloc] initWithSocketURL:url config:@{@"log": @YES, @"compress": @YES}];
    self.socket = self.manager.defaultSocket;
    [self.socket on:@"connecting" callback:^(NSArray* data, SocketAckEmitter* ack) {
        // this method not call.... while statusChange is connecting
        NSLog(@"socket connecting");
    }];
    [self.socket on:@"statusChange" callback:^(NSArray* data, SocketAckEmitter* ack) {
        //when excute this method crash
        NSInteger status = [[data firstObject] integerValue];
        if (status == SocketIOStatusConnecting) {
            //do other thing.
        }
    }];
    [self.socket connect];
}

###logs

2019-01-12 00:08:07.190984+0800 ModelO[2953:125439] LOG SocketIOClient{/}: Adding handler for event: connecting
2019-01-12 00:08:07.191311+0800 ModelO[2953:125439] LOG SocketIOClient{/}: Adding handler for event: statusChange
2019-01-12 00:08:07.191565+0800 ModelO[2953:125439] LOG SocketIOClient{/}: Handling event: statusChange with data: [connecting]
2019-01-12 00:08:15.044914+0800 ModelO[2953:125439] -[_SwiftValue integerValue]: unrecognized selector sent to instance 0x600001118990
2019-01-12 00:08:15.048586+0800 ModelO[2953:125439] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[_SwiftValue integerValue]: unrecognized selector sent to instance 0x600001118990'
*** First throw call stack:
(
	0   CoreFoundation                      0x000000010dc5f1bb __exceptionPreprocess + 331
	1   libobjc.A.dylib                     0x000000010d1fd735 objc_exception_throw + 48
	2   CoreFoundation                      0x000000010dc7df44 -[NSObject(NSObject) doesNotRecognizeSelector:] + 132
	3   CoreFoundation                      0x000000010dc63ed6 ___forwarding___ + 1446
	4   CoreFoundation                      0x000000010dc65da8 _CF_forwarding_prep_0 + 120
	5   ModelO                              0x000000010c756c0b __27-[ViewController configure]_block_invoke_2 + 123
	6   SocketIO                            0x000000010ca77c43 $SSo7NSArrayC8SocketIO0B10AckEmitterCIeyByy_SayypGAEIeggg_TR + 67
	7   SocketIO                            0x000000010ca77cad $SSo7NSArrayC8SocketIO0B10AckEmitterCIeyByy_SayypGAEIeggg_TRTA + 13
	8   SocketIO                            0x000000010ca6b646 $S8SocketIO0A12EventHandlerV15executeCallback4with0G3Ack0gA0ySayypG_SiAA0A8IOClientCtF + 166
	9   SocketIO                            0x000000010ca7517f $S8SocketIO0A8IOClientC11handleEvent_4data17isInternalMessage7withAckySS_SayypGSbSitF + 2111
	10  SocketIO                            0x000000010ca74916 $S8SocketIO0A8IOClientC17handleClientEvent_4datayAA0aeF0O_SayypGtF + 134
	11  SocketIO                            0x000000010ca6fe27 $S8SocketIO0A8IOClientC6statusAA0A8IOStatusOvW + 247
	12  SocketIO                            0x000000010ca6fd1a $S8SocketIO0A8IOClientC6statusAA0A8IOStatusOvs + 218
	13  SocketIO                            0x000000010ca71108 $S8SocketIO0A8IOClientC7connect12timeoutAfter11withHandlerySd_yycSgtF + 664
	14  SocketIO                            0x000000010ca70e1b $S8SocketIO0A8IOClientC7connectyyF + 59
	15  SocketIO                            0x000000010ca70e54 $S8SocketIO0A8IOClientC7connectyyFTo + 36
	16  ModelO                              0x000000010c756aaf -[ViewController configure] + 831
	17  ModelO                              0x000000010c756769 -[ViewController viewDidAppear:] + 89
	18  UIKitCore                           0x0000000116978544 -[UIViewController _setViewAppearState:isAnimating:] + 947
	19  UIKitCore                           0x000000011697b27d __64-[UIViewController viewDidMoveToWindow:shouldAppearOrDisappear:]_block_invoke + 42
	20  UIKitCore                           0x00000001169795e2 -[UIViewController _executeAfterAppearanceBlock] + 78
	21  UIKitCore                           0x0000000116f9d20e _runAfterCACommitDeferredBlocks + 634
	22  UIKitCore                           0x0000000116f8bb2c _cleanUpAfterCAFlushAndRunDeferredBlocks + 384
	23  UIKitCore                           0x0000000116fac2de __34-[UIApplication _firstCommitBlock]_block_invoke_2 + 153
	24  CoreFoundation                      0x000000010dbc462c __CFRUNLOOP_IS_CALLING_OUT_TO_A_BLOCK__ + 12
	25  CoreFoundation                      0x000000010dbc3de0 __CFRunLoopDoBlocks + 336
	26  CoreFoundation                      0x000000010dbbe654 __CFRunLoopRun + 1284
	27  CoreFoundation                      0x000000010dbbde11 CFRunLoopRunSpecific + 625
	28  GraphicsServices                    0x00000001143d21dd GSEventRunModal + 62
	29  UIKitCore                           0x0000000116f9181d UIApplicationMain + 140
	30  ModelO                              0x000000010c7574d0 main + 112
	31  libdyld.dylib                       0x00000001100b8575 start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb) 

When [self.socket on:@"statusChange" callback:^{ NSInteger status = [[data firstObject] integerValue];}] is called , crashed. because [data firstObject] is a _SwiftValue. and [self.socket on:@"connecting" callback:^(NSArray* data, SocketAckEmitter* ack) {}] is not called when connect status is connecting;
Is there something wrong with my demo configure? Or do I have to set other build options?

env : pod 'Socket.IO-Client-Swift','~> 13.3.0'

@nuclearace
Copy link
Member

Hm, this "statusChange" issue may be an issue with the bridged API. I'm going to need to investigate further.

@nuclearace nuclearace added the bug label Jan 11, 2019
@nuclearace
Copy link
Member

Okay, I think I see how to fix this. It will require a minor version bump. Basically it looks like if, in Swift, you pass an @objc enum to Objective-C, you must pass the rawValue instead of the Swift enum.

nuclearace added a commit that referenced this issue Jan 11, 2019
@nuclearace nuclearace mentioned this issue Jan 11, 2019
@webMing
Copy link
Author

webMing commented Jan 12, 2019

Okay, I think I see how to fix this. It will require a minor version bump. Basically it looks like if, in Swift, you pass an @objc enum to Objective-C, you must pass the rawValue instead of the Swift enum.

En,Solving is really fast And another thing that confuses me is why I focus on 'connecting ,connect, disconect ...' those events but those method not call when socket`s connect status did change. What i think is that the socket`s connect status change and if you you implement socket on "connenting" method it should call.

@webMing
Copy link
Author

webMing commented Jan 12, 2019

When will you release the new version ? 😁

@nuclearace
Copy link
Member

@webMing I plan on releasing a new version this week.

nuclearace added a commit that referenced this issue Jan 16, 2019
* development:
  update readme and changelog for new version
  add changelog entry for exponential backoff
  config setting and test updates for reconnectWaitMax and randomizationFactor
  add configurable exponential backoff
  Fix #1147
  Bump Starscream to 3.0.6
  Update to Swift 4.2
@webMing
Copy link
Author

webMing commented Jan 17, 2019

Hi nuclearace,I find this problem still exists. I`m sure my new version is v14.0.0

The _SwiftValue type still not convert any OC type. @nuclearace

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants