Why is this code not recognising the NSString as being equal?
use [string isEqualToString:@"any string"]
See a very useful discussion here: Understanding NSString comparison
Understanding NSString comparison
The reason why ==
works is because of pointer comparison. When you define a constant NSString
using @""
, the compiler uniquifies the reference. When the same constants are defined in other places in your code, they will all point to the same actual location in memory.
When comparing NSString
instances, you should use the isEqualToString:
method:
NSString *myString1 = @"foo";
NSString *myString2 = @"foo";
NSString *myString3 = [[NSString alloc] initWithString:@"foo"];
NSLog(@"%d", (myString2 == myString3)) //0
NSLog(@"%d", (myString1 == myString2)); //1
NSLog(@"%d", [myString1 isEqualToString:myString2]); //1
NSLog(@"%d", [myString1 isEqualToString:myString3]); //1
[myString3 release];
Edit:
NSString *myString3 = [[NSString alloc] initWithString:@"foo"];
// this is same with @"foo"
initWithString:
does not create a new reference any more, you will need initWithFormat
,
NSString *myString3 = [[NSString alloc] initWithFormat:@"foo"];
Why isn't string comparison returning true?
Operation == is comparing pointers in this case. Obviously you are comparing two different objects: @"YES"
which is a literal string and the object returned by [defaults valueForKey:@"giveHint"]
method call.
If you need to compare strings by their content use [[defaults valueForKey:@"giveHint"] isEqualToString:@"YES"]
IF statement in Objective-C in Xcode
'=' is for assignment while '==' is for comparison. But in the case of string comparison you should use isEqualToString
method. Something like this:
if ([magicButton.titleLabel.text isEqualToString: @"1"]) {
[magicButton setTitle:@"2" forState:UIControlStateNormal];
}
PS. Also note that you should get the UILabel
's text
property
Categories in Objective-C aren't working
It really seems like your category isn't being compiled or linked into the same target that you're using it from. You should make sure that NSData+Base64.m is marked to be compiled by the same target that it's being used from by getting info on the two files and comparing the targets they're assigned to.
A test you can perform is to add a line with an #error error message to NSData+Base64.m, which will cause the build to fail when it gets to that file. Like this:
#error We're now compiling NSData+Base64.m
Then look and see which target fails to compile.
Got is not a recognized Objective-C method when bridging Swift to React-Native
This is a part of Swift 3's changes and can be solved by adding an underscore:
import Foundation
@objc(SwitchManager)
class SwitchManager: NSObject {
@objc func show(_ name: String) {
NSLog("%@", name);
}
}
See Swift 3's 0046 Proposal: Establish consistent label behavior across all parameters including first labels that is called out in the Swift.org migration guide under "Consistent first argument labels".
Basically, how Objective-C sees Swift methods has changed with Swift 3.
EDIT: This is still the case in Swift 4, see docs here under Omitting Argument Labels.
Converting NSData to NSString in Objective c
The docs for NSString
says
https://developer.apple.com/documentation/foundation/nsstring/1416374-initwithdata
Return Value An NSString object
initialized by converting the bytes in
data into Unicode characters using
encoding. The returned object may be
different from the original receiver.
Returns nil if the initialization
fails for some reason (for example if
data does not represent valid data for
encoding).
You should try other encoding to check if it solves your problem
// The following constants are provided by NSString as possible string encodings.
enum {
NSASCIIStringEncoding = 1,
NSNEXTSTEPStringEncoding = 2,
NSJapaneseEUCStringEncoding = 3,
NSUTF8StringEncoding = 4,
NSISOLatin1StringEncoding = 5,
NSSymbolStringEncoding = 6,
NSNonLossyASCIIStringEncoding = 7,
NSShiftJISStringEncoding = 8,
NSISOLatin2StringEncoding = 9,
NSUnicodeStringEncoding = 10,
NSWindowsCP1251StringEncoding = 11,
NSWindowsCP1252StringEncoding = 12,
NSWindowsCP1253StringEncoding = 13,
NSWindowsCP1254StringEncoding = 14,
NSWindowsCP1250StringEncoding = 15,
NSISO2022JPStringEncoding = 21,
NSMacOSRomanStringEncoding = 30,
NSUTF16StringEncoding = NSUnicodeStringEncoding,
NSUTF16BigEndianStringEncoding = 0x90000100,
NSUTF16LittleEndianStringEncoding = 0x94000100,
NSUTF32StringEncoding = 0x8c000100,
NSUTF32BigEndianStringEncoding = 0x98000100,
NSUTF32LittleEndianStringEncoding = 0x9c000100,
NSProprietaryStringEncoding = 65536
};
Related Topics
Swiftui - Animations Triggered Inside a View That's in a List Doesn't Animate the List as Well
How to Edit Uialertaction Text Font Size and Color
Get the Frame of Uibarbuttonitem in Swift
How to Send Multiple Parameterts to PHP Server in Http Post
How to Get Hour and Minutes from Nsdate
Find Current Country from iPhone Device
How to Open the Imagepicker in Swiftui
Uitextview: Disable Selection, Allow Links
Swift - Compressing Video Files
Embed a Uiviewcontroller in a Navigationcontroller Using Segues
Swift Force-Unwrapping Exception Not Propagated
iPhone Different Screen Sizes in Flash? (Getting Black Bars)
Is There Any Way of Asking an iOS View Which of Its Children Has First Responder Status
Delete Specified File from Document Directory