In one of my projects, one of the external modules (SDK) fails to compile with the following error :
Semantic issue
Typedef `MyEnumType` cannot be referenced with a enum specifier
But it works fine with my other projects.
Here`s the header file where the error comes from :
typedef NS_ENUM(NSUInteger, MyEnumType) {
value1,
value2,
value3,
};
#protocol MyProtocol <NSObject>
- (void)myFunction:(enum MyEnumType)type; // Compilation error shows here
#end
Since it works in other projects, I guess something is wrong in my project build settings ?
Any help would be appreciated, thanks
When trying to build my xcode project, I get an error at this line:
- (NSArray<H24CommercialSlide * >*)allSlides;
Giving me a Parse issue: Expected '>' & Expected ')'
Not sure how to resolve this issue and what has changed in xcode that makes this previously working code fail.
Any ideas?
Full code snippet:
#import <Foundation/Foundation.h>
#class H24CommercialSlide;
#protocol H24SlidesProviderProtocol <NSObject>
- (NSArray<H24CommercialSlide * >*)allSlides;
#end
#interface H24SlidesProvider : NSObject<H24SlidesProviderProtocol>
#end
Lightweight generics were introduced in Xcode 7. Install Xcode 7 or change
- (NSArray<H24CommercialSlide * >*)allSlides;
to
- (NSArray*)allSlides;
I'm writing the receipt validation code in swift, but I have a problem with a PKCS7_type_is_signed macro :
Use of unresolved identifier 'PKCS7_type_is_signed'
Are there any way to use it in Swift except of creating Objective-C wrapper for this macros ?
Wrapper looks like this :
#import "OpenSSLWrapper.h"
#import "openssl/pkcs7.h"
#import "openssl/objects.h"
#implementation OpenSSLWrapper
+ (BOOL)PKCS7TypeIsSigned:(PKCS7*)bio{
return PKCS7_type_is_signed(bio);
}
#end
The macro won't work in Swift. You must write a wrapper.
Swift cannot discern type information from a macro. What sort of arguments should the Swift compiler allow to be passed into that macro? Because it cannot discern, it will not compile it.
It's also probably worth mentioning that in C/Objective-C, these macros are simple find & replace. The macro is expanded before compilation. If this macro did expand, it'd almost certainly expand into code that wouldn't compile in a .swift file.
From Apples
Using Swift with Cocoa and Objective-C
Complex macros are used in C and Objective-C but have no counterpart
in Swift. Complex macros are macros that do not define constants,
including parenthesized, function-like macros. You use complex macros
in C and Objective-C to avoid type-checking constraints or to avoid
retyping large amounts of boilerplate code. However, macros can make
debugging and refactoring difficult. In Swift, you can use functions
and generics to achieve the same results without any compromises.
Therefore, the complex macros that are in C and Objective-C source
files are not made available to your Swift code.
And as pointed by nhgrif (thank you :-) ), 2 options here use wrapper or just expand macro.
Expand Macro :
# define PKCS7_type_is_signed(a) (OBJ_obj2nid((a)->type) == NID_pkcs7_signed)
func PKCS7_type_is_signed(pkcs:UnsafeMutablePointer<PKCS7>)->Bool{
return OBJ_obj2nid(pkcs.memory.type) == NID_pkcs7_signed
}
func PKCS7_type_is_data(pkcs:UnsafeMutablePointer<PKCS7>)->Bool{
return (OBJ_obj2nid(pkcs.memory.type) == NID_pkcs7_data)
}
Wrapper :
.h file:
#import <Foundation/Foundation.h>
#import "openssl/pkcs7.h"
#import "openssl/objects.h"
#interface OpenSSLWrapper:NSObject
+ (BOOL)PKCS7TypeIsSigned:(PKCS7*)bio;
+ (BOOL)PKCS7TypeIsData:(PKCS7*)contents;
#end
.m file :
#import "OpenSSLWrapper.h"
#implementation OpenSSLWrapper
+ (BOOL)PKCS7TypeIsSigned:(PKCS7*)bio{
return PKCS7_type_is_signed(bio);
}
+ (BOOL)PKCS7TypeIsData:(PKCS7*)contents{
return PKCS7_type_is_data(contents);
}
#end
I am trying to declare a designated initializer like this:
- (instancetype)initWithDelegate:(id <MyDelegate>)delegate NS_DESIGNATED_INITIALIZER;
But it is showing me this compilation error:
Expected ':'
Interestingly when I try to write it like this (reference link: Adopting Modern Objective-C) -
- (instancetype)init NS_DESIGNATED_INITIALIZER;
It shows this error:
Expected ';' after method prototype.
Any ideas on how to properly use NS_DESIGNATED_INITIALIZER?
NS_DESIGNATED_INITIALIZER macro is not defined in the library headers for Xcode 5 - you need Xcode 6 to use it. Note your link says "Pre-release".
The macro is defined in the following way (quoting NSObjCRuntime.h)
#ifndef NS_DESIGNATED_INITIALIZER
#if __has_attribute(objc_designated_initializer)
#define NS_DESIGNATED_INITIALIZER __attribute__((objc_designated_initializer))
#else
#define NS_DESIGNATED_INITIALIZER
#endif
#endif
Note you can still use
- (instancetype)initWithDelegate:(id <MyDelegate>)delegate __attribute__((objc_designated_initializer));
in Xcode 5 or you can add that macro explicitly to your precompiled header.
Bear in mind you can only add this to interface or class extension declarations otherwise you'll get this error:
'objc_designated_initializer' attribute only applies to init methods of interface or class extension declarations
I'd like to do the following in Xcode:
Find all NSLog commands without comments, and replace it with //NSLog...
In other words, I want to comment all NSLog calls. Is this possible? Is there an easy way to do this?
wait there is far more simple method. Just add the following lines when you dont need NSLOG to your constants file
#define NSLog //
and comment it when you need it.
EDIT: In latest Xcode, you get error on above statement. So I figured out the best way is
#define NSLog(...)
Update:
The answer bellow is actually much better. See here.
Initial answer:
There is a little hack that you could do. Search for all NSLog and replace them with //NSLog and than do another search for ////NSLog and replace them with //NSLog.
#define NSLog if(1) NSLog
if you dont want log set 1 as 0.
I have to do a separate answer because I cant comment yet, but one thing you really need to be careful about when you do a find and replace is something like this:
if(BOOL)
NSLog(#"blah blah");
[self doSomething];
If you comment out the nslog, the conditional gets associated with the method below it, correct me if I'm wrong
The answers you have are correct for your question. But. Your real question is how to turn of NSLogs in certain conditions. i.e. you want them to show for Debug builds, and not for Release builds. In which case try defining and using the DLog() macro as described on Cocoa Is My Girlfriend. If you're using Xcode4 it's even easier because the Debug and Release builds define and undefine DEBUG so you don't have to do that.
It's a lot easier than commenting and uncommenting lines of code.
#ifdef RELEASE
#define NSLog(...) do { } while (0)
#endif
is the best way i found so far.
#define NSLog(...)
add this line into your .pch file
if you want log than comment it
You can do this in a single find and replace operation. You can just do this simple Regular Expression replace. This handles both the commented(//) and non-commented lines. This also works even if the previous commented lines has more than two forward slashes(like ///)instead of rwo. You can refer this link. Do the following steps.
Select Edit > Find > Find and Replace in Workspace
Style => Regular Expression
Type (/)*(NSLog.*) in Find field.
Do the find operation.
Type //\2 in the Replace field.
Do the replace operation.
Enjoy the beauty of regular expressions ;-)
I would do this
#define EnableNSLog 1
#if EnableNSLog == 0
#define NSLog //
#elif EnableNSLog == 1
#warning Disable NSLog
#endif
This should generate a warning message to remind me to disable NSLog before final release.
right click on NSLog statement in xcode and select "find in project" as text.you would be prompted to a new window where you can follow the guidance given by Mihai Fratu.
TNQ
in the Menu bar : Edit > Find > Find and Replace in Workspace
then, display options to use regular expressions.
search/replace for "[^/]NSLog"
How to disable NSLog in Xcode for Production stage
Add #define NSLog in appName-Prefix.pch file in Supporting Files Folder of your project
and result file code look like...
// Prefix header for all source files of the 'NSLog' target in the 'NSLog' project
//
#import <Availability.h>
#ifndef __IPHONE_4_0
#warning "This project uses features only available in iOS SDK 4.0 and later."
#endif
#ifdef __OBJC__
#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>
#endif
//Add this to disable NSLog
#define NSLog //
You can use following Preprocessor Directives, it will not go with release mode.
So you don't have to commenting NSLog().
#ifdef DEBUG
NSLog(#"YOUR MESSAGE HERE!");
#endif
try this also:
#define NSLog(#"YOUR MESSAGE HERE!") do { } while (0)
Add the following line to your .pch file
#define NSLog
for enable comment it