This is an Objective-C macOS project created with Xcode version 13.2.1. Inside the project, I have a templated class named "plistModifier". The class is meant to set custom types of values for any plist (e.g. NSString
, NSNumber
etc.). The header file of the class is called "plistModifier.h" and it looks like this:
#ifndef plistModifier_h
#define plistModifier_h
#include <iostream>
#include <string>
#import <Foundation/Foundation.h>
#include <unistd.h>
template <class type>
class plistModifier {
public:
void modifyPref(std::string key, type value);
type getPref(std::string key);
};
#endif /* plistModifier_h */
The implementation of the class is as follows in a seperate "plistModifier.mm" file:
#include "plistModifier.h"
template <class type>
void plistModifier<type>::modifyPref(std::string key, type val) {
NSString* preferencePlist = [[[NSBundle mainBundle] resourcePath] stringByAppendingString:@"/com.rA9.LeetDownPreferences.plist"];
NSDictionary* dict=[[NSDictionary alloc] initWithContentsOfFile:preferencePlist];
[dict setValue:val forKey:[NSString stringWithUTF8String:val]];
[dict writeToFile:preferencePlist atomically:YES];
}
template <class type>
type plistModifier<type>::getPref(std::string key) {
NSString *preferencePlist = [[[NSBundle mainBundle] resourcePath] stringByAppendingString:@"/com.rA9.LeetDownPreferences.plist"];
NSDictionary *dict=[[NSDictionary alloc] initWithContentsOfFile:preferencePlist];
return dict[key];
}
The issue is, when I create a plistModifier object and call it's methods, the compiler throws the error Undefined symbols for architecture x86_64: "plistModifier<int>::modifyPref(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, int)", referenced from: -[SettingsVC debuggingToggle:] in SettingsVC.o
This is how I call the methods of the object in SettingsVC.mm
file:
#import "SettingsVC.h"
#include "plistModifier.h"
plistModifier<int> plistObject;
- (IBAction)debuggingToggle:(id)sender {
plistObject.modifyPref("DebugEnabled", _debugToggle.state);
}
I have tried using the same templated class in an empty C++ project, and it didn't give me any errors. What do you think the problem is?